w3resource

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:

import java.util.Scanner;

public class ArmstrongNumberUsingLoops {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Taking user input
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        int originalNumber = number;
        int result = 0;
        int numberOfDigits = 0;

        // Find the number of digits in the input number
        int temp = number;
        while (temp != 0) {
            temp /= 10;
            numberOfDigits++;
        }

        // Calculate the sum of each digit raised to the power of the number of digits
        while (number != 0) {
            int digit = number % 10;
            result += Math.pow(digit, numberOfDigits); // Raise the digit to the power of numberOfDigits
            number /= 10;
        }

        // Check if the sum equals the original number
        if (result == originalNumber) {
            System.out.println(originalNumber + " is an Armstrong number.");
        } else {
            System.out.println(originalNumber + " is not an Armstrong number.");
        }

        scanner.close(); // Close the scanner resource
    }
}

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:

import java.util.Scanner;

public class ArmstrongNumberUsingRecursion {

    // Recursive function to calculate the sum of digits raised to the power
    public static int armstrongSum(int number, int numberOfDigits) {
        if (number == 0) {
            return 0;
        }
        int digit = number % 10;
        return (int)Math.pow(digit, numberOfDigits) + armstrongSum(number / 10, numberOfDigits);
    }

    // Helper function to count the number of digits
    public static int countDigits(int number) {
        if (number == 0) {
            return 0;
        }
        return 1 + countDigits(number / 10);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Taking user input
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        int originalNumber = number;

        // Find the number of digits
        int numberOfDigits = countDigits(number);

        // Check if the sum of the powers equals the original number
        if (armstrongSum(number, numberOfDigits) == originalNumber) {
            System.out.println(originalNumber + " is an Armstrong number.");
        } else {
            System.out.println(originalNumber + " is not an Armstrong number.");
        }

        scanner.close(); // Close the scanner resource
    }
}  

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:




Follow us on Facebook and Twitter for latest update.