Program that reads a integer number and check whether it is Amstrong number or not using function.

import java.io.*;
class number
{ int a,x,n,sum=0;
boolean flag=true;
void amstrong(int x)
{   n=x;
while(flag)
{
a=x%10;
x=x/10;
sum+=(a*a*a);
if(x==0 )
flag=false;
}
if(sum==n) System.out.println("Numner is Amstrong.  :)");
else  System.out.println("Number is not Amstrong.  :(");
}
}
class test
{ public static void main(String[] asd)throws IOException
{
BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
int n;
String num;
number a=new number();
System.out.println("Enter a number::");
num=obj.readLine();
n=Integer.parseInt(num);
a.amstrong(n);

}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s