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

Create a Java source code program that accepts a number from a user. Based on th

ID: 3534764 • Letter: C

Question

Create a Java source code program that accepts a number from a user. Based on the number, the program will check the prime numbers in the range between 1 and the number entered by the user. The program finds and displays the number of prime numbers in the range, stores the prime numbers in an array, and displays the prime numbers. For instance, as shown in the following figure, if a user entered 10, the program finds the number of prime numbers (e.g., 4), stores the prime numbers in an array internally (i.e., in the computer memory), and displays the prime numbers (2, 3, 5, 7).
C:Code> java Test1YourName 10 Between 1 and 10, there are 4 prime numbers. They are: 2 3 5 7
If the user entered 20, the program will find and display the number of prime numbers (8), store the prime numbers in an array, and display the prime numbers (2, 3, 5, 7, 11, 13, 17, 19). Create a Java source code program that accepts a number from a user. Based on the number, the program will check the prime numbers in the range between 1 and the number entered by the user. The program finds and displays the number of prime numbers in the range, stores the prime numbers in an array, and displays the prime numbers. For instance, as shown in the following figure, if a user entered 10, the program finds the number of prime numbers (e.g., 4), stores the prime numbers in an array internally (i.e., in the computer memory), and displays the prime numbers (2, 3, 5, 7).
C:Code> java Test1YourName 10 Between 1 and 10, there are 4 prime numbers. They are: 2 3 5 7
If the user entered 20, the program will find and display the number of prime numbers (8), store the prime numbers in an array, and display the prime numbers (2, 3, 5, 7, 11, 13, 17, 19). C:Code> java Test1YourName 10 Between 1 and 10, there are 4 prime numbers. They are: 2 3 5 7
If the user entered 20, the program will find and display the number of prime numbers (8), store the prime numbers in an array, and display the prime numbers (2, 3, 5, 7, 11, 13, 17, 19).

Explanation / Answer

100% working code with sample output



import java.util.*;


class FindPrime{

static boolean isPrime(long number) {

int count = 0;

boolean isPrime = false;

int i = (int) Math.ceil(Math.sqrt(number));

while (i > 1) {

if ((number != i) && (number % i == 0)) {

isPrime = false;

break;

} else if (!isPrime)

isPrime = true;

count++;

--i;

}

return isPrime;

}


public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter a number: ");

int count = 0;

long num = input.nextLong();


for (int i = 1; i < num; i++) {

if (isPrime(i)) {

count++;

System.out.println(i);

}

}System.out.println("There are total " + count + " prime numbers in this range");

}

}



Sample output


Enter a number: 20

2

3

5

7

11

13

17

19

There are total 8 prime numbers in this range