Question
The program prompts the user to input an integer. Try to find a divisor for this integer, if there are no divisors the integer is
The program prompts the user to input an integer. Try to find a divisor for this integer, if there are no divisors the integer is a prime number, otherwise it is not a prime number.
*/
import java.util.Scanner;
public class Lab5Part3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
int divisor;
System.out.println("Enter an integer greater than 1");
num = input.nextInt();
divisor = num / 2;
while (num % divisor != 0) {
System.out.println("Trying factor " + divisor);
divisor--;
}
if (divisor == 1) {
System.out.print("The input integer " + num + " is a prime
number");
} else {
System.out.print("The input integer " + num + " is divisible
by " + divisor);
}
}
/* Answer the following questions about this code:
* 1. What is the loop control variable?
* 2. What statement initializes the loop control variable?
* 3. What is the loop condition?
* 4. What statement updates the loop control variable?
*
* After you answer above questions, comment out the main method
given and
write your own main method. In your main method use a "for" loop
instead of the while loop.
*/
}
/*
* Copy and paste your program output here
*/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started