Armstrong Number is number that is equal to sum of cubes of its digits
For Example: 0,1,153,370,371
Let us try to understand why 153 is Armstrong Number:
153= (1*1*1)+(5*5*5)+(3*3*3)
where:
TOTAL IS: 153
370 =(3*3*3)+(7*7*7)+(0*0*0)
Where:
class ArmstrongNumber{
For Example: 0,1,153,370,371
Let us try to understand why 153 is Armstrong Number:
153= (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
5*5*5)=125
(3*3*3)=27
TOTAL IS: 153
Let us try to understand why 370 is Armstrong Number:
370 =(3*3*3)+(7*7*7)+(0*0*0)
Where:
(3*3*3)=27
(7*7*7)=343
(0*0*0)=0
Total is: 370
Now Let us see to check Armstrong Number:
Ex:
class ArmstrongNumber{
public static void main(String args[]){
int sum=0,a,temp;
int n=153;// it is number to check armstrong number
temp=n;
while(n>0){
a=n%10;
n=n/10;
sum=sum+(a*a*a);
sum=sum+(a*a*a);
}
if(temp==sum){
System.out.println("Armstrong number");
System.out.println("Armstrong number");
else{
System.out.println("not Armstrong number");
}
}
Output:
Armstrong number
No comments:
Post a Comment