Sunday, November 15, 2015

Java Program To Find Armstrong Number or Not

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:  
(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);
}
if(temp==sum){
System.out.println("Armstrong number");
else{
System.out.println("not Armstrong number");
}
}

Output:        
Armstrong number    
        














No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...