Header Ads

Java program to print perfect numbers from 1 to 1000 | What is perfect Number | Programming in Java

 Java program to print perfect numbers from 1 to 1000

What is a perfect number mean: if the sum of factors excluding the number

itself is equal to the number then that number is called a perfect number

ex. 496: factors are 2,4,8,16,31,62,124 and 248

2+4+8+16+31+62+124+248 = 496=number so this is the perfect number




package java_Practice;


public class Perfect_number_from_1_to_1000 {


public static void main(String[] args) {

for(int n=1;n<=1000;n++)

{

int i=1,sum=0;

while(i<n)

{

if(n%i==0)

{

sum=sum+i;

}

i++;

}

if(sum==n)

{

System.out.println(n + " ");

}

}

}


}

No comments

Powered by Blogger.