on Leave a Comment

Java Program to Check Armstrong Number

This java program checks whether a number is Armstrong number or not. A number is Armstrong number, if the sum of cube of its digits is equal to that number. Example of Armstrong numbers, 407, 153, 371 etc.

For example, 407 is Armstrong number because
(4*4*4) = 64
(0*0*0) = 0
(7*7*7) = 343

Now, (64+0+343) = 407 

Java Program to Check Armstrong Number

import java.util.Scanner;
class ArmstrongNumber
{  
  public static void main(String [] args)  {  
    int a, temp, cube=0;  
    System.out.print("Enter a number : ");
    Scanner s = new Scanner(System.in);
    int num = s.nextInt(); 
    temp=num;  
    while(num>0)  
    {  
    a=num%10;  
    num=num/10;  
    cube=cube+(a*a*a);  
    }  
    if(temp==cube)  
    System.out.println(temp+" is a Armstrong number");   
    else  
        System.out.println(temp+"is not a Armstrong number");   
   }  
}  

Output:

Enter a number : 407
407 is a Armstrong number

0 comments:

Post a Comment