package factors;
import java.util.Scanner;
public class Factors {
private int data;
private boolean [] factors;
private int [] arr;
private int x;
Factors(int d){
data = d;
factors = new boolean[data];
arr = new int[data];
x=0;
}
void populate(){
int n = 1;
int f = 0;
while(n<=data){
if(data %n==0){
arr[f] = n;
factors[f] = true;
f++;
}
n++;
}
}
void display(){
System.out.printf("Factors of %d are: ", data);
for(int i=0; i<arr.length; i++){
if(arr[i]==0){
}
else{
System.out.printf("%d, ",arr[i]);
x++;
}
}
}
void perfect_number(){
int sum = 0;
int n = x- 2;
for(int i=0; i<=n;i++ ){
sum += arr[i];
}
if(sum==data){
System.out.printf("\n%d, is a perfect number\n", data);
}
}
public static void main(String[] args) {
System.out.println("Enter a number\n");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
Factors f = new Factors(n);
f.populate();
f.display();
f.perfect_number();
}
}
Comments
Leave a comment