Java Program to Check Armstrong Number

Introduction

An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits, each raised to the power of the number of digits. For example, 153 is an Armstrong number because (1^3 + 5^3 + 3^3 = 153). This guide will show you how to create a Java program that checks whether a given number is an Armstrong number.

Problem Statement

Create a Java program that:

  • Takes an integer input from the user.
  • Checks if the number is an Armstrong number.
  • Displays the result indicating whether the number is an Armstrong number.

Example 1:

  • Input: 153
  • Output: 153 is an Armstrong number.

Example 2:

  • Input: 9474
  • Output: 9474 is an Armstrong number.

Example 3:

  • Input: 123
  • Output: 123 is not an Armstrong number.

Solution Steps

  1. Prompt for Input: Use the Scanner class to read an integer input from the user.
  2. Calculate the Number of Digits: Determine the number of digits in the input number.
  3. Compute the Sum of Digits Raised to Power: Raise each digit to the power of the total number of digits and compute the sum.
  4. Check for Armstrong Number: Compare the computed sum to the original number.
  5. Display the Result: Print whether the number is an Armstrong number.

Java Program

import java.util.Scanner;

/**
 * Java Program to Check Armstrong Number
 * Author: https://www.javaguides.net/
 */
public class ArmstrongNumberChecker {

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

        // Step 1: Take input from the user
        System.out.print("Enter a number to check if it is an Armstrong number: ");
        int number = scanner.nextInt();

        // Step 2: Calculate the number of digits
        int numberOfDigits = String.valueOf(number).length();

        // Step 3: Compute the sum of digits raised to the power of numberOfDigits
        int sum = 0;
        int temp = number;
        while (temp > 0) {
            int digit = temp % 10;
            sum += Math.pow(digit, numberOfDigits);
            temp /= 10;
        }

        // Step 4: Check if the sum is equal to the original number
        if (sum == number) {
            System.out.println(number + " is an Armstrong number.");
        } else {
            System.out.println(number + " is not an Armstrong number.");
        }
    }
}

Explanation

Step 1: Prompt for Input

  • The program prompts the user to enter a number.

Step 2: Calculate the Number of Digits

  • The number of digits is calculated using String.valueOf(number).length(), which converts the number to a string and then returns the length of that string.

Step 3: Compute the Sum of Digits Raised to Power

  • The program iterates over each digit of the number, raising each digit to the power of the number of digits and summing the results.

Step 4: Check for Armstrong Number

  • The sum of the digits raised to the power is compared to the original number. If they are equal, the number is an Armstrong number.

Step 5: Display the Result

  • The program prints whether the number is an Armstrong number or not.

Output Examples

Example 1:

Enter a number to check if it is an Armstrong number: 153
153 is an Armstrong number.

Example 2:

Enter a number to check if it is an Armstrong number: 9474
9474 is an Armstrong number.

Example 3:

Enter a number to check if it is an Armstrong number: 123
123 is not an Armstrong number.

Conclusion

This Java program efficiently checks whether a given number is an Armstrong number by calculating the sum of its digits raised to the power of the number of digits and comparing the result to the original number. This method is simple and effective for determining if a number qualifies as an Armstrong number.

Comments