Perfect Number Logic In Java
class PerfectNumber
{
public static void main(String[] args)
{
int sum=0, num=28, i;
for(i=1;i<num;i++)
{
if(num%i==0)
sum=sum+i;
}
if(sum==num)
{
System.out.println(""+num+" is a PERFECT NUMBER");
}
else
{
System.out.println(""+num+" is a not a PERFECT NUMBER");
}
}
}
Perfect Number is a number whose factors add up for the number itself.
suppose number is x and its factors are a,b,c.
Then x will be considered as Perfect number if.
a+b+c=x
factors of 28 are 1,2,4,7,14.
&
1+2+4+7+14 =28
Hence 28 is a Perfect Number
0 comments:
Post a Comment