Armstrong number or not using Java
Armstrong or not using Java :
An Armstrong Number of three digits is the number whose sum of the cubes of its digits is equal to the given number or can say the number itself should be the result.
Let us consider an example for understanding it in a better way.
- Suppose a number is 407 ;
- Cubes of its digits are :
Cube of 4 = 64 ;
Cube of 0 = 0 ;
Cube of 7 = 343 ;
- Sum of all digit’s cube :
64 + 0 + 343 =407
Here the calculated result is equal to the given number ;
So, 407 is an Armstrong Number.
In this article we will create a java program to check whether a given number is an Armstrong number or not.
Step 1 : Ask the user to enter a number.
Step 2 : Declare two variables, one for storing sum and second for storing the copy of the given number so that the changes we make that should not reflect in the original number.
Step 3 : Use a loop till number is not equal to 0.
Step 4 : Continuously pick out last digit of the number till the number exist.
Step 5 : Calculate and store the sum of the cubes of its digits one by one.
Step 6 : Repeat Steps 3 to 5 till condition for loop does not get false.
Step 7 : Compare whether the sum is equal to the given number or not.
- If this gets true , then the given number is an Armstrong Number.
- Else the number is not an Armstrong Number.
Code in Java :
//Java program to check whether a number is armstrong or not
public class armstrong_number_or_not
public static void main(String args)
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
int n = number;
int sum = 0;
while(n != 0)
int pick_last = n % 10;
sum = sum + (pick_last * pick_last * pick_last);
n = n / 10;
//condition for checking that the sum is equal to the number or not
if(sum == number)
System.out.println("Not an Armstrong Number");
//closing scanner class(not compulsory, but good practice)
Enter a number : 153
Enter a number : 123
Not an Armstrong Number