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.