Search Your Topic

config
Thursday, 18 May 2017 03:26

Armstrong Number in Java

Written by
Rate this item
(0 votes)

Armstrong Number in Java: Armstrong number is a number that is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

Let's try to understand why 153 is an Armstrong number.

 
  1. 153 = (1*1*1)+(5*5*5)+(3*3*3)  
  2. where:  
  3. (1*1*1)=1  
  4. (5*5*5)=125  
  5. (3*3*3)=27  
  6. So:  
  7. 1+125+27=153  

Let's try to understand why 371 is an Armstrong number.

 
  1. 371 = (3*3*3)+(7*7*7)+(1*1*1)  
  2. where:  
  3. (3*3*3)=27  
  4. (7*7*7)=343  
  5. (1*1*1)=1  
  6. So:  
  7. 27+343+1=371  

Let's see the java program to check Armstrong Number.

 
  1. class ArmstrongExample{  
  2.   public static void main(String[] args)  {  
  3.     int c=0,a,temp;  
  4.     int n=153;//It is the number to check armstrong  
  5.     temp=n;  
  6.     while(n>0)  
  7.     {  
  8.     a=n%10;  
  9.     n=n/10;  
  10.     c=c+(a*a*a);  
  11.     }  
  12.     if(temp==c)  
  13.     System.out.println("armstrong number");   
  14.     else  
  15.         System.out.println("Not armstrong number");   
  16.    }  
  17. }  

Output:

armstrong number

 

Read 219 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

Java Training

Facebook