question archive Can someone help me with this Java program?   This program is to help a student learn their prime numbers

Can someone help me with this Java program?   This program is to help a student learn their prime numbers

Subject:Computer SciencePrice:9.82 Bought3

Can someone help me with this Java program?

 

This program is to help a student learn their prime numbers. Generate a random number (int) in the range of 1 - 200.  Ask the user if the number is a prime number.  Determine whether the number is prime (a prime number is a number that can only be divided by itself and 1) and then as output let the user know whether they were right or not.  If the number is prime, let them know that there are no numbers that will divide evenly into the number.  If the number is not prime, provide them with a list of the numbers that will divide evenly into the number. Do not list 1 or the number in that list. Be sure to ask the user if they would like to play again.  You'll use what is known as counter logic, assume the number is prime and then loop from 2 to the number minus - 1 and if any of those numbers can be divided into the number and leave no remainder, then the number is not prime.

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

import java.util.Random;
import java.util.Scanner;
public class Main {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        Random r=new Random();
        while(true){
            int n=r.nextInt(200)+1;
            System.out.print(n+" is prime or not?(Yes/No): ");
            String answer=sc.next();
            int c=0;
            for(int i=2;i<n;i++){
                if(n%i==0){
                    c+=1;
                }
            }
            if((c==0 && answer.equals("Yes")) || (c>0 && answer.equals("No"))){
                System.out.println("You are correct");
            }
            else{
                System.out.println("You are wrong");
            }
            if(c>0){
                System.out.println(n+" is not a prime number");
                System.out.print("Numbers evenly divide "+n+" are:");
                for(int i=2;i<n;i++){
                    if(n%i==0)
                    System.out.print(" "+i);
                }
                System.out.println();
            }
            else{
                System.out.println(n+" is a prime number");
            }
            System.out.print("Do you want to continue?(Yes/No): ");
            String ch=sc.next();
            if(ch.equals("No"))
            break;
        }
    }
}

Step-by-step explanation

Output:

152 is prime or not?(Yes/No): No
You are correct
152 is not a prime number
Numbers evenly divide 152 are: 2 4 8 19 38 76
Do you want to continue?(Yes/No): Yes
45 is prime or not?(Yes/No): No
You are correct
45 is not a prime number
Numbers evenly divide 45 are: 3 5 9 15
Do you want to continue?(Yes/No): Yes
66 is prime or not?(Yes/No): No
You are correct
66 is not a prime number
Numbers evenly divide 66 are: 2 3 6 11 22 33
Do you want to continue?(Yes/No): Yes
161 is prime or not?(Yes/No): Yes
You are wrong
161 is not a prime number
Numbers evenly divide 161 are: 7 23
Do you want to continue?(Yes/No): Yes
32 is prime or not?(Yes/No): Yes
You are wrong
32 is not a prime number
Numbers evenly divide 32 are: 2 4 8 16
Do you want to continue?(Yes/No): Yes
82 is prime or not?(Yes/No): No
You are correct
82 is not a prime number
Numbers evenly divide 82 are: 2 41
Do you want to continue?(Yes/No): Yes
46 is prime or not?(Yes/No): No
46 is not a prime number
Numbers evenly divide 46 are: 2 23
Do you want to continue?(Yes/No): Yes
89 is prime or not?(Yes/No): Yes
You are correct
89 is a prime number
Do you want to continue?(Yes/No): No