Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i\'m in java 1 and i nedd help with this code. pleae follow the given directions

ID: 3671115 • Letter: I

Question

i'm in java 1 and i nedd help with this code. pleae follow the given directions and test the program to see if it works with the give input.

Programming Concepts

1. forLoop
2. do..whileLoop 3. breakstatement

Assignment Description

You will write a program that determines whether or not a number is prime. A prime number is a number that is divisible only by 1 and itself.

Examples:

7: 1 * 7 (Prime)

5: 1 * 5 (Prime)

6: 1 * 6, 2 * 3 (Not Prime)

12: 1 * 12, 2 * 6, 3 * 4 (Not Prime)

Assignment Requirements

1. You must check to see that the user enters a positive number a. Use a do..while loop to accomplish this.

Use a for loop to determine if the number is prime.

Use a boolean value to keep track of the prime value of the number.

Assignment Suggestions

Declare all necessary variables.
a. intvariable: Tostoreuserinput
b. boolean variable: To keep track if the value is prime or not

Use a do..while loop to validate that the user entered a positive number.

Useaforlooptoloopfrom2tothenumber–1.

a. If the number is divisible by the counter in the for loop: (remember modulus!)

Change the value of the boolean value (the number is not prime)

Exit the loop

4. Depending on the value of the boolean value,

a. Output whether or not the number is prime.

User input is underlined

Enter a positive number to determine if a number is prime: -13

Enter a positive number to determine if a number is prime: 0

Enter a positive number to determine if a number is prime: -4

Enter a positive number to determine if a number is prime: 7

7 is prime.

user@loki:~$ java PrimeNum

Enter a positive number to determine if a number is prime: 4

4 is not prime.

Explanation / Answer


import java.util.Scanner;//imported to read int as input from console
public class prime{
  
public static void main(String[] argv)
{
    int num,i;//declaring variables,'num' is to take user input.. 'i' as incrementing variable(counter) in for loop..
    boolean value=true;//boolean variable to keep track if the value is prime or not..initializing it to "true"...assuming that given value is prime..
    do//do..while loop to validate that the user entered a positive number... it runs upto the value entered by the user is positive..
    {
    System.out.print("Enter a positive number to determine if a number is prime:");
    Scanner in = new Scanner(System.in);//scanning user input
    num = in.nextInt();//storing in num variable..
    }while(num<=0);// it is a positive value then this condition fails and loop breaks..
    //using this for loop.. checking weather other than 1 and number itself, it is divible by any other number, between 1 and itself..by using counter variable 'i'..
    for(i=2;i<num;i++) {
        if(num%i==0) {//If the number is divisible by the counter variable 'i'
            value=false;//Changing the value of the boolean value (the number is not prime)
            break;//exiting loop using break statement..
        }
    }
    if(value==true)
    {
        System.out.println(num+" is prime.");//if value is 'true' then it is prime
    }
    else
    {
        System.out.println(num+" is not a prime.");//if value is 'false' then it is not a prime
    }
}
}