Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/purvgwtw/public_html/tutorials/common.php on line 7
Java Perfect Number Program
Sponsered Links
Categories
Sponsered Links

Java Perfect Number Program

Through the given code, we are finding the perfect number from between 1 to 30. Perfect Numbers are the numbers whose sum of factors is equal to the number itself.

public class PerfectNumberExample{

public static void main(String[]args){
int sum=0, x=0;
for(int num=1;num<30;num++){
for(int i=1;i<num;i++){
x=num%i;
if(x==0)
sum=sum+i;
}
if(sum==num){
System.out.println("Perfect Number is: "+num);
System.out.println("Factors are: ");
for(int i=1;i<num;i++){
x=num%i;
if(x==0)
System.out.println(i);
}
}
sum=0;
}
}
}

Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2014 PURVACOMPUTERS.COM All Right Reserved