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

) A prime number is a positive integer divisible only by itself and 1. A number

ID: 3588148 • Letter: #

Question

) A prime number is a positive integer divisible only by itself and 1. A number which is not prime is composite. For example, 2, 3, and 5 are prime, but 4 and 6 are composite. A perfect number, p, is a positive integer that equals the sum of its divisors, excluding p itself. For example, 6 is a perfect number because the divisors of 6 (1, 2, and 3) add up to 6. Write a program which looks for primes and perfect numbers as follows. Prompt the user to enter the minimum value to check, and ensure that the user enters an integer greater than 1. Next, prompt the user to enter the maximum value to check, and ensure that the user enters an integer greater than the minimum value he/she just entered. For each integer x between the minimum and maximum values to check (inclusive), print a list of all the divisors of x starting from 1 up to x-1. Then print whether x is prime or composite. Next, print whether x is perfect or not perfect. Finally, print a count of the number of primes found and a count of the number of perfect numbers found. Here is the output from an example run of the program: Let's search for prime numbers and perfect numbers! Enter minimum value to check (an integer greater than 1): 0 That entry was not valid. Please be sure to enter an integer greater than 1. Enter minimum value to check (an integer greater than 1): 1 That entry was not valid. Please be sure to enter an integer greater than 1. Enter minimum value to check (an integer greater than 1): 2 Enter maximum value to check (an integer greater than the minimum value to check): 1 That entry was not valid. Please be sure to enter an integer greater than the minimum value to check. Enter maximum value to check (an integer greater than the minimum value to check): 2 That entry was not valid. Please be sure to enter an integer greater than the minimum value to check. Enter maximum value to check (an integer greater than the minimum value to check): 6 2 is divisible by: 1 2 is prime. 2 is not perfect. 3 is divisible by: 1 3 is prime. 3 is not perfect. 4 is divisible by: 1 2 4 is composite. 4 is not perfect. 5 is divisible by: 1 5 is prime. 5 is not perfect. 6 is divisible by: 1 2 3 6 is composite. 6 is perfect. Primes found: 3 Perfect numbers found: 1 [JAVA]

Explanation / Answer

package org.students;

import java.util.Scanner;

public class PrimeCompositeCount {

public static void main(String[] args) {

//Declaring variables

int min, max, cntPrime = 0, cntPerfect = 0;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

System.out.println("Let's search for prime numbers and perfect numbers!");

/* This while loop continues to execute

* until the user enters a valid number

*/

while (true) {

System.out.print(" Enter minimum value to check (an integer greater than 1): ");

min = sc.nextInt();

if (min <= 1) {

System.out.println("That entry was not valid.");

System.out.println("Please be sure to enter an integer greater than 1.");

continue;

} else

break;

}

/* This while loop continues to execute

* until the user enters a valid number

*/

while (true) {

System.out.print(" Enter maximum value to check (an integer greater than the minimum value to check): ");

max = sc.nextInt();

if (max <= min) {

System.out.println("That entry was not valid.");

System.out.println("Please be sure to enter an integer greater than the minimum value to check.");

continue;

} else

break;

}

/* Check the number is prime or composite and a Number is Perfect Number or not .

* Divisiors of an Number and count prime and perfect numbers

*/

for (int start = min; start <= max; start++) {

if (isPrime(start)) {

System.out.println(start + " is Prime.");

cntPrime++;

} else {

System.out.println(start + " is composite.");

}

if (is_perfect(start)) {

cntPerfect++;

System.out.println(start + " is perfect.");

} else {

System.out.println(start + " is not perfect.");

}

System.out.print(start + " is divisible by :");

checkDivisiors(start);

}

System.out.println("Primes found:" + cntPrime);

System.out.println("Perfect numbers found:" + cntPerfect);

}

private static void checkDivisiors(int start) {

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

if (start % i == 0) {

System.out.print(i + " ");

}

}

System.out.println();

}

/* This method will check whether the number is prime or not

* Params :integer type number

* Return :boolean value.

*/

public static boolean isPrime(int number) {

if (number < 2)

return false;

// If the user entered number is '2' return true

else if (number == 2)

return true;

for (int i = 2; i * i <= number; i++) {

if (number % i == 0)

return false;

}

return true;

}

/* Function implementation which checks

* whether the user entered number is perfect or not

*/

public static boolean is_perfect(int num) {

//Declaring the variable

int sum = 0;

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

if (num % i == 0)

sum += i;

}

/* if the user entered number is equal to the

* sum value the number is perfect number

*/

if (sum == num)

return true;

else

return false;

}

}

___________________

Output:

Let's search for prime numbers and perfect numbers!

Enter minimum value to check (an integer greater than 1): 0
That entry was not valid.
Please be sure to enter an integer greater than 1.

Enter minimum value to check (an integer greater than 1): 1
That entry was not valid.
Please be sure to enter an integer greater than 1.

Enter minimum value to check (an integer greater than 1): 2

Enter maximum value to check (an integer greater than the minimum value to check): 2
That entry was not valid.
Please be sure to enter an integer greater than the minimum value to check.

Enter maximum value to check (an integer greater than the minimum value to check): 6
2 is Prime.
2 is not perfect.
2 is divisible by :1
3 is Prime.
3 is not perfect.
3 is divisible by :1
4 is composite.
4 is not perfect.
4 is divisible by :1 2
5 is Prime.
5 is not perfect.
5 is divisible by :1
6 is composite.
6 is perfect.
6 is divisible by :1 2 3
Primes found:3
Perfect numbers found:1

_____________Could you rate me well.Plz .Thank You