A Perfect number is a positive integer that is equal to the sum of its proper divisors except itself. Let's take an easy example, such as 6. 1, 2, 3 and 6 are the divisors of 6. If we add up all the numbers except 6, we end with the sum of 6 itself.
import java.util.Scanner;
class perfect
{//class perfect opens
public static void main(String[]args)
{//main method opens
Scanner sc=new Scanner(System.in);
System.out.println("Enter a no");
int n=sc.nextInt();//variable n to accept the no
int s=0;//variable s to store the sum of the factors
for(int i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
if(s==n)
System.out.println("It is a perfect no");
else
System.out.println("It is not a perfect no");
}//main method closes
}//class closes
No comments:
Post a Comment
any problem in any program comment:-