
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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. This article aims to explain Java programs that checks whether the given number is Armstrong or not.
How to Check Armstrong Number?
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.
Example Scenarios:
Let's discuss the problem statement with the help of some examples:
Input = 1634 Output: 1634 is an Armstrong number
Here, 1^4 + 6^4 + 3^4 + 4^4 = 1634; therefore, the sum of powers and the number are same.
Next Instance:
Input = 525 Output: 525 is not an Armstrong number
Here, 5^3 + 2^3 + 5^3 = 258, sum of powers and the number are not same.
Checking Armstrong Number in Java
In Java, use the following ways to check if the given number is an Armstrong or not:
Using Loops
Follow the given steps to find an Armstrong number using while and for loops:
-
First, declare a variable to store the sum of powers of the digits.
-
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 a string so that we can iterate through each digit of a 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 the power of each digit is equal to the original number or not.
Example
In the following example, we are using for and while loops to check Armstrong number in Java:
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 loop runs until num becomes 0 while(num != 0) { // find last digit int rem = num % 10; int mul = 1; // for loop to calculate rem raised to the power of len for(int i = 1; i <= len; i++) { mul *= rem; } // addition sum += mul; // remove last digit num /= 10; } if(sum == copy) { System.out.println(copy + " is a Armstrong number"); } else { System.out.println(copy + " is not an Armstrong number"); } } }
The above code will produce the following result:
The number defined to check Armstrong is: 153 153 is an Armstrong number
Using Math.pow() Method
The Math.pow() method accepts two arguments, base and exponent, and returns the value of the base raised to the power of the exponent. We will use this method, instead of a for loop, to calculate the power.
Example
This is another example to check Armstrong number where we use the Math.pow() method.
public class IsArmstrong { public static void main(String[] args) { int sum = 0; int num = 525; 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; // using Math.pow sum += (int) Math.pow(rem, len); num /= 10; } if(sum == copy) { System.out.println(copy + " is a Armstrong number"); } else { System.out.println(copy + " is not an Armstrong number"); } } }
Output of the above code is as:
The number defined to check Armstrong is: 525 525 is not an Armstrong number
Using Recursion
Since checking an Armstrong number needs loops, as we have seen in the previous two examples, we can use recursion to replace them. Loops repeat the same task as long as required, and recursion is also used when the same task needs to be repeated multiple times.
Example
In this Java program, we use recursion to check Armstrong number.
public class ArmstrongExample { public static void main(String[] args) { int sum = 0; int num = 1634; int copy = num; System.out.println("The number defined to check Armstrong is: " + num); String n = Integer.toString(num); int len = n.length(); // calling recursive function sum = IsArmstrong(num, len); if(sum == copy) { System.out.println(copy + " is a Armstrong number"); } else { System.out.println(copy + " is not an Armstrong number"); } } // recursive function public static int IsArmstrong(int num, int len) { if (num == 0) { return 0; } int rem = num % 10; return (int) Math.pow(rem, len) + IsArmstrong(num / 10, len); } }
Since 1634 is an Armstrong number, the output will be:
The number defined to check Armstrong is: 1634 1634 is a Armstrong number