Program to check Armstrong number

 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 say, 153 = (1*1*1)+(5*5*5)+(3*3*3)  


package javabox;

class ArmstrongExample{  

  public static void main(String[] args)  {  
  
    int c=0,a,temp;  
    int n=153; //Number to check armstrong  
    temp=n;  
    while(n>0)  
    {  
    a=n%10;  
    n=n/10;  
    c=c+(a*a*a);  
    }  
    if(temp==c)  
    System.out.println("Armstrong number");   
    else  
        System.out.println("Not Armstrong number");   
   }  

}  

Output - 
Armstrong number

No comments:

Post a Comment

Basic structure and printing elements of Linked list in java

class Node {      int data ;      Node next ;           Node ( int data ){            this . data = data ;            ...