A perfect number is a positive number greater than 1 whose factors (including 1
ID: 3861431 • Letter: A
Question
A perfect number is a positive number greater than 1 whose factors (including 1 but not the number itself) add up to the number itself. For instance, 6 is a perfect number because the factors of 6 add up to 6: 1 2 3 equals 6. Likewise, 28 is a perfect number because 1 2 4 7 14 equals 28. 26 is not a perfect number because 1 2 13 is not equal to 26 Write a public static method called testPerfect() which takes one integer argument and returns a boolean value of true or false if the argument is or isn't a perfect number. Your method cannot simply use a switch statement or nested if-else structure to determine if it's a perfect number or not. In other words, you may not write code like the following if number 28 II number 496 I number 6 IInumber 8128 return true; else return false; You must calculate the summation of the factors to see if they add up to the number itself. This method will not perform any output. Write another public static method printFactors0 that also takes an integer argument and prints out the factors of the argument in descending order (largest to smallest). By necessity, this method must perform output. Similarly to the testPerfect method, you may not simply use and if-else statement to output the factors inthis method. If a number is a perfect number, the output should follow the format: NUMBER FACTORS where NUMBER is the user entered, followed immediately by a colon, followed immediately by a single-space-separated list of the FACTORS. If the number is not perfect, the output should follow the format: NUMBER NOT PERFECT where NUMBER is the user entered, followed immediately by a colon, followed immediately the text NOT PERFECT.Explanation / Answer
import java.util.Scanner;
public class Perfect
{
public static void printFactors(int n)
{
for(int i = n-1; i>=1; i--)
{
if(n % i == 0)
{
System.out.print(i+" ");
}
}
}
public static boolean testPerfect(int n)
{
int sum=0;
for(int i = 1; i < n; i++)
{
if(n % i == 0)
{
sum = sum + i;
}
}
if(sum == n&&n>0)
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args)
{
boolean flag=true;
int n=0;
Scanner sc=new Scanner(System.in);
while(flag)
{
System.out.println("How many numbers would you like to test?");
n=sc.nextInt();
if(n<=0)
{
}else
{
flag=false;
}
}
for (int i = 0; i < n; i++)
{
System.out.println(" Please enter a possible perfect number:");
int num=sc.nextInt();
if(testPerfect(num))
{
System.out.print(num+": ");
printFactors(num);
}
else
{
System.out.print(num+" :NOT PERFECT");
}
}
}
}
==================================================================================
Output:
How many numbers would you like to test?
-1
How many numbers would you like to test?
0
How many numbers would you like to test?
-99
How many numbers would you like to test?
10
Please enter a possible perfect number:
1
1 :NOT PERFECT
Please enter a possible perfect number:
0
0 :NOT PERFECT
Please enter a possible perfect number:
-2
-2 :NOT PERFECT
Please enter a possible perfect number:
6
6: 3 2 1
Please enter a possible perfect number:
28
28: 14 7 4 2 1
Please enter a possible perfect number:
496
496: 248 124 62 31 16 8 4 2 1
Please enter a possible perfect number:
9
9 :NOT PERFECT
Please enter a possible perfect number:
9999
9999 :NOT PERFECT
Please enter a possible perfect number:
3
3 :NOT PERFECT
Please enter a possible perfect number:
31
31 :NOT PERFECT
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.