An Armstrong number is a number in which the sum of its digits cube is equal to that number itself.
For Example :
Number = 153
1^3 + 5^3 +3^3 = 153
(1 + 125 + 27 = 153)
In the above example ,153 is a 3-digit number and sum of cubes of its digits is equal to the original number.
Few Armstrong Numbers are : 1, 153, 370, 371, 407, 1634, 8208, 9474, ...
Program
import java.util.*;
public class armstrong_num
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int r,sum=0;
int p=0;
int a=n;
while(a>0)
{
p++;
a=a/10;
}
a=n;
while(n>0)
{
r=n%10;
sum+=Math.pow(r,p);
n=n/10;
}
if(sum==a)
System.out.println(a+" is an Armstrong number");
else
System.out.println(a+" is not an Armstrong number");
}
}
إرسال تعليق