Tuesday 27 October 2015

Program in Java to find whether a number is perfect or not

Perfect Number-  If the sum of all the factors of a number is equal to the number itself then it is a perfect number. This program finds whether a given number is perfect number or not.

Example:- 6 = 1+2+3
28 = 1+2+4+7+14

import java.io.*;
class perfectnumber
{
public static void main()throws IOException
{
int n,s=0;
InputStreamReader IR=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(IR);
System.out.println("entre a no");
n=Integer.parseInt(br.readLine());
 
for(int x=1;x<n;x++)
if(n%x==0)
s=s+x;
if(s==n)
System.out.println("it is perfect no");
else
System.out.println("it is not perfect no");
}
}

No comments:

Post a Comment