Java Project - Armstrong Number Checker
Armstrong Number Checker - Loops and Recursion Solutions:
Armstrong Number Checker :
Check if a number is an Armstrong number.
This project checks if a number is an Armstrong number (a number equal to the sum of its digits each raised to the power of the number of digits). The user inputs a number, and the program returns whether it is an Armstrong number.
Input: A number.
Output: Whether the number is an Armstrong number or not.
Example:
- Input: 153
- Output: "Armstrong number"
- Input: 123
- Output: "Not an Armstrong number"
Solution 1: Armstrong Number Checker Using Loops
Code:
Output:
Enter a number: 153 153 is an Armstrong number.
Enter a number: 237 237 is not an Armstrong number
Explanation :
- Input: The program prompts the user to enter a number.
- Count Digits: A loop is used to count the number of digits in the input number.
- Armstrong Calculation: The program calculates the sum of each digit raised to the power of the number of digits.
- Check Armstrong: If the calculated sum equals the original number, it is an Armstrong number; otherwise, it is not.
- Close Scanner: The scanner is closed to free up system resources.
Solution 2: Armstrong Number Checker using Recursion
Code:
Output:
Enter a number: 370 370 is an Armstrong number.
Enter a number: 371 371 is an Armstrong number.
Explanation:
- Input: The program prompts the user to enter a number.
- Recursive Count Digits: A recursive method is used to count the number of digits.
- Recursive Armstrong Calculation: A recursive method calculates the sum of the digits raised to the power of the number of digits.
- Check Armstrong: The calculated sum is compared to the original number to determine if it is an Armstrong number.
- Close Scanner: The scanner is closed to free up system resources.
Java Code Editor: