import java.io.*;
class Amstrong
{
public static void main(String args[]) throws IOException
{
int num,temp,n,sum=0;
System.out.println("enter your number:\n");
DataInputStream din=new DataInputStream(System.in);
num=Integer.parseInt(din.readLine());
temp=num;
while(temp>0)
{
n++;
temp/=10;
}
temp=num;
while(temp>0)
{
sum+=pow(temp%10,n);
temp/=10;
}
if(sum==num)
{
System.out.println(num+" is an amstrong number");
}
else
{
System.out.println(num+" is not an amstrong number");
}
}
int pow(int a,int b)
{
int product=1;
for(int i=0;i<b;i++)
{
product*=a;
}
return product;
}
}
Output of above Program is:
class Amstrong
{
public static void main(String args[]) throws IOException
{
int num,temp,n,sum=0;
System.out.println("enter your number:\n");
DataInputStream din=new DataInputStream(System.in);
num=Integer.parseInt(din.readLine());
temp=num;
while(temp>0)
{
n++;
temp/=10;
}
temp=num;
while(temp>0)
{
sum+=pow(temp%10,n);
temp/=10;
}
if(sum==num)
{
System.out.println(num+" is an amstrong number");
}
else
{
System.out.println(num+" is not an amstrong number");
}
}
int pow(int a,int b)
{
int product=1;
for(int i=0;i<b;i++)
{
product*=a;
}
return product;
}
}
Output of above Program is:
0 comments:
Post a Comment