You are on page 1of 2

Class perfect

1/1

/**
* QUESTION
*
* Design a program in java to check whether a number
* is a PERFECT number or not.
*/
import java.io.*;
public class perfect
{
public int digit_count(int n)throws IOException
/** method to count no of digts in a number */
{
int c=0;
int m=n;
//creating dummy variable
while(m>0)
{
c=c+1; //counter variable
m=m/10; //updating loop variable
}
return c; //returning no of digits
}//end of digit_count
public void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the number");
int n=Integer.parseInt(d.readLine());
int c=digit_count(n);
//counting digits
int p=(int)Math.pow(n,2);
//squaring the number
int e=(int)Math.pow(10,c);
int ex=p%e;
//extracting c no of digits from end of square of number
if(ex==n)
{
System.out.println("The number is a PERFECT number");
}
else
{
System.out.println("The number is not a PERFECT number");
}
}
}

Jan 16, 2015 10:59:04 PM

perfect1.main();
enter the number
25
The number is a PERFECT number
perfect1.main();
enter the number
35
The number is not a PERFECT number

You might also like