Armstrong number, also known as the narcissistic number, is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 13 + 53 + 33 = 153. In this blog, we will discuss how to find Armstrong numbers in Java using different types of loops and recursion.
What is the Armstrong number in Java and example?
An Armstrong number in Java is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 13 + 53 + 33 = 153.
Check out our blog Factorial Program in Java, Now!
5 Methods to Find Armstrong Number in Java
We will look at five different methods for finding Armstrong numbers in Java, each with its own set of advantages and disadvantages.
A. Armstrong number in Java using while loop
import java.util.Scanner; public class ArmstrongWhileLoop { public static void main(String[] args) { int number, originalNumber, remainder, result = 0, n = 0; Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); number = scanner.nextInt(); originalNumber = number; while (originalNumber != 0) { originalNumber /= 10; ++n; } originalNumber = number; while (originalNumber != 0) { remainder = originalNumber % 10; result += Math.pow(remainder, n); originalNumber /= 10; } if (result == number) System.out.println(number + " is an Armstrong number."); else System.out.println(number + " is not an Armstrong number."); } }
Explanation of the code:
In the above code, we take a number from the user using the Scanner class. We then calculate the number of digits in the number using a while loop. The variable ‘n’ keeps track of the number of digits in the number. We then calculate the sum of the digits each raised to the power of ‘n’ using another while loop. The variable ‘result’ stores the sum. Finally, we check if the sum is equal to the original number or not and display the result accordingly.
Output:
If we input 153 as the number, the output will be:
Enter a number: 153
153 is an Armstrong number.
B. Armstrong number in Java without loop
import java.util.Scanner; public class ArmstrongWithoutLoop { public static void main(String[] args) { int number, originalNumber, remainder, result = 0, n = 0; Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); number = scanner.nextInt(); originalNumber = number; while (originalNumber != 0) { originalNumber /= 10; ++n; } originalNumber = number; for (; originalNumber != 0; originalNumber /= 10) { remainder = originalNumber % 10; result += Math.pow(remainder, n); } if (result == number) System.out.println(number + " is an Armstrong number."); else System.out.println(number + " is not an Armstrong number."); } }
Explanation of the code:
This code is similar to the previous code, except we use a for loop instead of a while loop to calculate the sum of the digits each raised to the power of ‘n’.
Output:
If we input 153 as the number, the output will be:
Enter a number: 153
153 is an Armstrong number.
An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 13 + 53 + 33 = 153.
Also, learn about Neon Number in Java here!
C. Here is the Java code to check if a number is an Armstrong number using a for loop
import java.util.Scanner; public class ArmstrongNumberForLoop { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); int originalNumber = number; int numberOfDigits = 0; int sum = 0; // Find the number of digits while (originalNumber != 0) { numberOfDigits++; originalNumber /= 10; } // Calculate sum of power of digits for (int i = number; i > 0; i /= 10) { int digit = i % 10; sum += Math.pow(digit, numberOfDigits); } if (sum == number) { System.out.println(number + " is an Armstrong number."); } else { System.out.println(number + " is not an Armstrong number."); } } }
Explanation of the code:
We first take the input number from the user using the Scanner class.
We store the original number in a separate variable for later use.
We find the number of digits in the input number using a while loop.
We then use a for loop to iterate through each digit of the input number and calculate the sum of the power of digits.
Finally, we check if the sum of the power of digits is equal to the input number or not. If it is equal, then the number is an Armstrong number.
Output:
The output of this code for the input number 153 would be:
Enter a number: 153
153 is an Armstrong number.
D. Armstrong number in Java using recursion
An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 13 + 53 + 33 = 153.
Here’s the Java code for finding Armstrong numbers using recursion:
public class ArmstrongRecursion { public static void main(String[] args) { int num = 153; if (isArmstrong(num, order(num))) System.out.println(num + " is an Armstrong number"); else System.out.println(num + " is not an Armstrong number"); } public static int order(int num) { int count = 0; while (num != 0) { count++; num /= 10; } return count; } public static boolean isArmstrong(int num, int order) { if (num == 0) return true; int digit = num % 10; return (int) Math.pow(digit, order) + isArmstrong(num / 10, order - 1); } }
In this code, the order method calculates the number of digits in the given number, and the isArmstrong method uses recursion to check if the number is an Armstrong number or not.
Output:
The output of this code for the input number 153 would be:
Enter a number: 153
153 is an Armstrong number.
Also, learn about Special Number in Java, Now!
E. Armstrong number in Java while do loop
public class ArmstrongWhileLoop { public static void main(String[] args) { int num = 153; int originalNum = num; int remainder, result = 0; int n = String.valueOf(num).length(); while (originalNum != 0) { remainder = originalNum % 10; result += Math.pow(remainder, n); originalNum /= 10; } if (result == num) System.out.println(num + " is an Armstrong number"); else System.out.println(num + " is not an Armstrong number"); } }
In this code, the while do loop calculates the sum of the digits raised to the power of the number of digits, and checks if the sum is equal to the original number.
Output:
The output of this code for the input number 153 would be:
Enter a number: 153
153 is an Armstrong number.
Armstrong Number in Java: Tips and Tricks
Here are 10 guidelines for creating efficient Java code when working with Armstrong numbers:
- our code will become more modular and simpler to read as a result.
- To separate specific digits from a number, use the modulus operator (%).
- To find the power of a number, use the Math.pow() method. Iterate through the number’s digits using a loop.
- The sum of the digits raised to the power of the number of digits should be kept in a variable.
- Make a string out of a number by using the String.valueOf() method.
- The String.length() method can be used to calculate a number’s digit count.
- You can turn a string into an integer by using the parseInt() method.
- To deal with potential errors when parsing integers from strings, use a try-catch block.
- To make your code clearer, use descriptive variable names.
Pay attention to pointers and techniques when writing Java code to work with Armstrong numbers, and use debugging tools to find and correct errors.
To learn about AI, Blockchain, and Metaverse, visit the Techaroha website.
In this article, we discussed how to find Armstrong numbers in Java and looked at 5 different approaches. Armstrong number computation has been covered using while loops, for loops, recursion, and other methods. Selecting the approach that best suits your needs and coding style is crucial because each method has advantages and disadvantages of its own.
Our blog post on “Armstrong Number in Java” is intended to help you with any Java-related questions you may have. Visit Newtum’s website to learn more about our online coding courses in Java, Python, PHP, and other topics as you continue to improve your coding abilities. You can learn programming concepts and develop into an expert Java developer with practice and perseverance.