Java Program to Check Armstrong Number


In number theory, an Armstrong number is a pattern based number whose sum of each digit raised to the power of total number of digits is equal to the given number itself. Hence, to check whether a number is an Armstrong or not, first, determine the total number of digits and assume it 'n'. Then separate each digit and raise them to the power of 'n'. In the last step, calculate the power of each digit and add all of them. If the sum we get is equal to the original number, then it is an Armstrong number otherwise not. This article aims to explain how we can check whether the given number is Armstrong or not in Java.

Java Program to Check Armstrong Number

In this section, we will write two Java programs to identify if the number is Armstrong number or not. Before that let's discuss the problem statement with the help of an example −

Instance

Input 1

1634

Output

1634 is an Armstrong number

Explanation

1^4 + 6^4 + 3^4 + 4^4 = 1634, sum of powers and the number both are same.

Input 2

525

Output

525 is not an Armstrong number 

Explanation

5^3 + 2^3 + 5^3 = 258, sum of powers and the number both are not same.

Now, let's jump into the Java programs to check if the given number is an Armstrong or not.

Example 1

In the following example, we will take an input number from the user with the help of Scanner class and check whether the given number is an Armstrong number or not.

Approach

  • First, declare a variable to store the sum of powers of the digits.

  • Then, create an instance of the Scanner class to take an integer input from the user.

  • Copy the original input to another variable. It is necessary because the original number will get decremented to 0 at the end of next while loop.

  • Now, convert the integer input to string so that we can iterate through each digit of number using the for loop.

  • Take a while loop that will run till the original number becomes 0. We will put the logic of checking Armstrong in this loop.

  • In the end, check if the sum of power of each digit is equal to the original number or not.

import java.util.*;
public class IsArmstrong {
   public static void main(String[] args) {
      // variable to store sum of powers
      int sum = 0;
      // creating instance of Scanner class
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a number to check Armstrong number: ");
      // to take input number
      int num = sc.nextInt();
      // copying the input to variable named copy
      int copy = num;
      // converting integer to string
      String n = Integer.toString(num);
      // storing the length of converted string
      int len = n.length();
      // loop to check armstrong number
      while(num != 0) {
         int rem = num % 10;
         int mul = 1;
         for(int i = 1; i <= len; i++) {
            mul *= rem;
         }
         sum += mul;
         num /= 10;
      }
      // to print the result
      if(sum == copy) {
         System.out.println(copy + " is an Armstrong number");
      } else {
         System.out.println(copy + " is not an Armstrong number");
      }
   }
}

Output 1

Enter a number to check Armstrong number: 
525
525 is not an Armstrong number

Output 2

Enter a number to check Armstrong number: 
1634
1634 is an Armstrong number

Example 2

This is another example to check Armstrong number where instead of taking input from user, we will initialize the input number at the time of declaration.

import java.util.*;
public class IsArmstrong {
   public static void main(String[] args) {
      int sum = 0;
      int num = 153;
      int copy = num;
      System.out.println("The number defined to check Armstrong is: " + num);
      String n = Integer.toString(num);
      int len = n.length();
      while(num != 0) {
         int rem = num % 10;
         int mul = 1;
         for(int i = 1; i <= len; i++) {
            mul *= rem;
         }
         sum += mul;
         num /= 10;
      }
      if(sum == copy) {
         System.out.println(copy + " is a Armstrong number");
      } else {
         System.out.println(copy + " is not an Armstrong number");
      }
   }
}

Output

The number defined to check Armstrong is: 153
153 is an Armstrong number

Conclusion

In this article, we have learned what is an Armstrong Number and how to check whether a given number is Armstrong or not. For this purpose, we have used while and for loops and also, if-else condition in our Java programs.

Updated on: 10-Aug-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements