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

Thanks, A prime number is a positive integer greater than 1 that is divisible on

ID: 3552263 • Letter: T

Question

Thanks,

A prime number is a positive integer greater than 1 that is divisible only by itself and 1. You are to write a program that repeatedly asks a user for an integer value and then displays all prime numbers less than or equal to that number. The program will prompt the user for a positive number, or a value of -1 to end the program. If the user enters 0, or a negative number, the program will re-prompt the user. Your program's behavior on input other than integers will not be tested. The program will determine, for each number from 2 to the number given by the user, whether that number is prime or not. In the columnar format presented below, the program will display information about both prime and non-prime numbers. When the table is complete the program will display the count of prime numbers in the given range and the ratio of prime numbers in the range, and then present the initial prompt again. Use (tab) to line up the columns.

Explanation / Answer

/* OUTPUT

Welcome to the prime number program !!
################################################
Please enter a positive integer (-1 to quit):6
Number         Is Prime     Counterexample
2            Yes         -
2            Yes         -
3            Yes         -
4                No           4 % 2 = 0
5            Yes         -
6                No           6 % 2 = 0
3 of first 6 integers are prime; prime ratio = 0.5
Please enter a positive integer (-1 to quit):0
Please enter a positive integer (-1 to quit):-2
Please enter a positive integer (-1 to quit):10
Number         Is Prime     Counterexample
2            Yes         -
2            Yes         -
3            Yes         -
4                No           4 % 2 = 0
5            Yes         -
6                No           6 % 2 = 0
7            Yes         -
8                No           8 % 2 = 0
9                No           9 % 3 = 0
10                No           10 % 2 = 0
4 of first 10 integers are prime; prime ratio = 0.4
Please enter a positive integer (-1 to quit):-1

*/

import java.util.*;
public class prime_number
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int number = 0;
System.out.println("Welcome to the prime number program !!");
System.out.println("################################################");
System.out.println("Please enter a positive integer (-1 to quit):");
number = in.nextInt();
while(number<=0 && number !=-1)
{
System.out.println("Please enter a positive integer (-1 to quit):");
number = in.nextInt();
}

while(number!=-1)
{
int count = 0;
System.out.println("Number         Is Prime     Counterexample");
System.out.println("2            Yes         -");
for(int i=2; i<=number; i++)
{
boolean is_prime = true;
for(int k=2; k*k<=i; k++)
{
if(i%k==0)
{
is_prime = false;
System.out.println(""+(i)+"                No"+"           "+i+" % "+k +" = 0");
}
}
if(is_prime)
{
count++;
System.out.println(""+i+"            Yes         -");
}
}
System.out.println(count + " of first "+ number +" integers are prime; prime ratio = "+ (double)(count)/number);
System.out.println("Please enter a positive integer (-1 to quit):");
number = in.nextInt();
while(number<=0 && number !=-1)
{
System.out.println("Please enter a positive integer (-1 to quit):");
number = in.nextInt();
}
}

} // end main.
}