Question

Write a program asking the user for an integer then find the lowest factor that is...

Write a program asking the user for an integer then find the lowest factor that is not a 2, 3, or 5. Hint: Use while or do-while with a flag. Think this way - you have found the answer when you find the lowest factor that is not divisible by 2, 3, or 5 (hint: %) but the number % factor = zero. Start factor at 2 then increment until the answer is found, then change the flag to stop the loop.****PLEASE USE DO WHILE OR WHILE LOOP AND A FLAG
0 0
Add a comment Improve this question Transcribed image text
Answer #1

As per the question, the code has to find the lowest factor which is not 2 or 3 or 5 and by that code the lowest factor of 28 is 4 but as the hint says find the lowest factor which is not divisible by 2 or 3 or 5 and by that code the lowest factor of 28 is 7.
So I have sent you both the codes, so that you choose the code which ever you want.
To find the factors of a user defined number, a loop is used in which a number is generated from 2 to the user defined number whose factor has to be find, and in the loop the user defined number is check whether it is divisible by generated number or not, if it is divisible then the generated number is the factor of the user defined number.

CODE AS PER THE QUESTION (find the lowest factor that is not a 2, 3, or 5):
import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter an integer: ");
int number = input.nextInt();
int factor = 2;
//flag is set to 0
int flag = 0;
int lowestfactor = 0;
do{
//factor that is not a 2, 3, or 5
if (factor != 2 && factor != 3 && factor != 5){
if (number % factor == 0){
//lowestfactor will be equal to factor
lowestfactor = factor;
//flag is changed so that loop stops
flag = flag+1;
}
}
factor = factor + 1;
}
while(flag==0);
System.out.print("Lowest factor of "+number+" is "+ lowestfactor);
   }
}
OUTPUT:

CODE AS PER THE HINT (find the lowest factor that is not divisible by 2, 3, or 5):
import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter an integer: ");
int number = input.nextInt();
int factor = 2;
//flag is set to 0
int flag = 0;
int lowestfactor = 0;
do{
if (number % factor == 0){
//factor that is not divisible by 2, 3, or 5
if (factor % 2 != 0 && factor % 3 != 0 && factor % 5 != 0){
//lowestfactor will be equal to factor
lowestfactor = factor;
//flag is changed so that loop stops
flag = flag+1;
}
}
factor = factor + 1;
}
while(flag==0 && factor<=number);
System.out.print("Lowest factor of "+number+" is "+ lowestfactor);
   }
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a program asking the user for an integer then find the lowest factor that is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program asking the user for an integer then find the lowest factor that is...

    Write a program asking the user for an integer then find the lowest factor that is not a 2, 3, or 5. Hint: Use while or do-while with a flag. Think this way - you have found the answer when you find the lowest factor that is not divisible by 2, 3, or 5 (hint: %) but the number % factor = zero. Start factor at 2 then increment until the answer is found, then change the flag to stop...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • Write a program in a script file that finds the smallest odd integer that is also...

    Write a program in a script file that finds the smallest odd integer that is also divisible by 3 and whose cube is greater than 4000. Use a loop in the program. The loop should start from 1 and stop when the number is found. The program should print the message: The required number is: [and then prints the number]

  • Write a complete program that will prompt the user for an integer You must keep asking...

    Write a complete program that will prompt the user for an integer You must keep asking the user until they give you a number that is greater than 700. Once they give a valid number, display all multiples of 7 from 7 to that number inclusively). You may assume the user will give you correct type of input, but your code must continue to prompt them until they give a number greater than 700.

  • Write a complete C++ program that do following. 1) Read a positive integer from the user...

    Write a complete C++ program that do following. 1) Read a positive integer from the user with proper prompt. Assume that this user is nice and he/she would not start the integer with digit 0. 2) Create a size 10 array as counters for digits 0 - 9. 3) Use a while loop for processing a) Single out the current last digit using modular 10 technique b) Increment the corresponding counter through the index derived in a). 4) At the...

  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 5. Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. Input Validation: Do not accept a negative starting number.

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to input 10 int numbers from the user...

  • Write a program that reads an integer k from user and finds the number of elements...

    Write a program that reads an integer k from user and finds the number of elements that are divisible by k in the file assignment4.txt. A number n is divisible by k if n = kx for some integer x > 0. You can use the mod operator % to test for divisibility. The file assign4.txt has integer values in the range [0,100 ]. You should use end-of-file controlled loop for this problem. Sample execution is given below. Assume the...

  • Exercise 9.2 Write a Python program that collects from the user a single integer. Have the...

    Exercise 9.2 Write a Python program that collects from the user a single integer. Have the program print “EVEN” if the integer is divisible by 2, and “ODD” if the integer is not divisible by two. [Use the % operator to compute the two’s modulus (remainder of division by two)] Exercise 9.3 Write a Python program that collects from the user two integers. Have the program print “Yes” if the two integers are BOTH greater than 10. Do nothing if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT