11.0k views
0 votes
6.24 (Perfect Numbers) An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a method isPerfect that determines whether parameter number is a perfect number. Use this method in an application that displays all the perfect numbers between1 and integer entered by the user. Display the factors of each perfect number to confirm that the number is indeed perfect. Challenge the computing power of your computer by testing numbers much larger than 1000. Display the results.SAMPLE RUN #4: java PerfectNumbersInteractive Session Standard Input Standard Error (empty) Standard Output Hide InvisiblesHighlight: NoneStandard Input OnlyPrompts OnlyStandard Output w/o PromptsFull Standard OutputAllShow Highlighted OnlyEnter·the·number·up·to·which·you·would·like·to·look·for·perfect·numbers:1000↵Looking·for·perfect·numbers·from·1·to·1000↵6·is·a·perfect·number·it's·factors·are:·1·2·3·↵28·is·a·perfect·number·it's·factors·are:·1·2·4·7·14·↵496·is·a·perfect·number·it's·factors·are:·1·2·4·8·16·31·62·124·248·↵

User Giacomo M
by
4.2k points

1 Answer

4 votes

Answer:

See explaination

Step-by-step explanation:

import java.util.Scanner;

public class PerfectNumFinder {

public static void main(String[] args) {

System.out.println("Enter the number up to which you would like to look for perfect numbers:");

int max = new Scanner(System.in).nextInt();

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

checkPerfect(num);

}

}

private static void checkPerfect(int num) {

int factors[]=new int[num];

int factorCount=0,sum=0;

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

if(num % i ==0){

factors[factorCount++]=i; //storing all the factors of a number into an array

sum+=i;

}

}

System.out.println();

if(sum==num){

System.out.println(num+" is a perfect number");

System.out.println("Factors of "+num+" are:");

for(int f=0;f<factorCount;f++)

System.out.print(factors[f]+" ");

}

}

}

Check out attachment for output.

6.24 (Perfect Numbers) An integer number is said to be a perfect number if its factors-example-1
User Zyoo
by
4.4k points