import java.util.Scanner;
class ArmstrongNumber
{
public static void main(String[] args)
{
int n,sum=0,digits=0,remainder,temp;
Scanner in = new Scanner(System.in);
System.out.println("Enter any number to check for armstrong");
n=in.nextInt();
temp=n;
// find the length of the number
while(temp!=0)
{
digits++;
temp=temp/10;
}
temp=n;
while(temp!=0)
{
remainder=temp%10;
sum=sum+power(remainder,digits);
temp=temp/10;
}
if (n == sum)
System.out.println(n + "is an armstrong number");
else
System.out.println( n + "is not a armstrong number");
}
static int power(int n, int r)
{
int c,p=1;
for (c=1; c<=r; c++)
p=p*n;
return p;
}
}
class ArmstrongNumber
{
public static void main(String[] args)
{
int n,sum=0,digits=0,remainder,temp;
Scanner in = new Scanner(System.in);
System.out.println("Enter any number to check for armstrong");
n=in.nextInt();
temp=n;
// find the length of the number
while(temp!=0)
{
digits++;
temp=temp/10;
}
temp=n;
while(temp!=0)
{
remainder=temp%10;
sum=sum+power(remainder,digits);
temp=temp/10;
}
if (n == sum)
System.out.println(n + "is an armstrong number");
else
System.out.println( n + "is not a armstrong number");
}
static int power(int n, int r)
{
int c,p=1;
for (c=1; c<=r; c++)
p=p*n;
return p;
}
}
No comments:
Post a Comment