You are here: Home / Topics / Program to check Armstrong number in Java

Program to check Armstrong number in Java

Filed under: Java on 2023-08-18 07:00:21

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

Code

// Program to check whether a given Number is
// ArmStrong Number or not.

class  ArmstrongTest
{
public static void main( String args[ ] )
{
 int r, n, x, sum;
 
 n = 153;
 x = n;
 
 sum = 0;
 while( n != 0 )
 {
  r = n % 10;
  sum = sum + ( r * r * r );
  n = n / 10;
 }
 
 if( sum == x )
  System.out.println( " Number " + x + " is a Amstrong Number." );
 else
  System.out.println( " Number " + x + " is NOT a Amstrong Number." );
}
}


Output:

Number 153 is a Amstrong Number.


About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.