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 to check whether a given number is an Armstrong number.

Problem Statement

Create a Java program that takes an integer input from the user and checks if 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. Take Input: Get the number 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. Output the Result: Display 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: ");
        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.");
        }
    }
}

Step by Step Explanation

  1. Take Input: The program starts by prompting the user to enter a number.

  2. Calculate the Number of Digits: The String.valueOf(number).length() is used to determine the number of digits in the input number. For example, if the input is 153, the number of digits is 3.

  3. Compute the Sum of Digits Raised to Power:

    • A temporary variable temp is used to store the original number.
    • The program iterates through each digit of the number by using the modulus operator % and integer division /.
    • For each digit, the program raises it to the power of numberOfDigits and adds the result to sum.
    • The loop continues until all digits have been processed.
  4. Check for Armstrong Number: After the loop, the sum of the digits raised to their respective powers is compared to the original number.

  5. Output the Result: If the sum equals the original number, it is an Armstrong number. Otherwise, it is not.

Output Examples

Example 1:

Enter a number: 153
153 is an Armstrong number.

Example 2:

Enter a number: 9474
9474 is an Armstrong number.

Example 3:

Enter a number: 123
123 is not an Armstrong number.

Conclusion

This Java program effectively checks whether a given number is an Armstrong number. The key steps involve calculating the number of digits, raising each digit to the power of the number of digits, summing these values, and comparing the sum to the original number. This program can be easily extended to check Armstrong numbers of different lengths or for different bases.

Related Java Programs with Output

  1. Java Program to Find Factorial Using Recursion
  2. Java Program to Find Sum of Natural Numbers
  3. Java Program to Reverse a Number
  4. Java Program to Find Prime Numbers
  5. Java Program to Check Armstrong Number
  6. Java Program to Count the Number of Digits in a Number
  7. Java Program for Prime Numbers Within a Range
  8. Java Program to Find the Largest of Three Numbers
  9. Java Program to Check if a Number is Prime
  10. Java Program to Find the Fibonacci Series
  11. Java Program to Check if a Number is Positive or Negative
  12. Java Program to Find GCD of Two Numbers
  13. Java Program to Find Strong Number
  14. Java Program to Find LCM of Two Numbers
  15. Java Program to Check Palindrome Number
  16. Java Program to Swap Two Numbers Without Using a Temp Variable
  17. Java Program to Find the Second Largest Number in an Array
  18. Java Program to Find GCD of Two Numbers
  19. Java Program to Find Strong Number
  20. Java Program to Find LCM of Two Numbers
  21. Java Program to Check Palindrome Number
  22. Java Program to Find the Largest of Three Numbers
  23. Java Program to Check if a Number is Prime
  24. Java Program to Swap Two Numbers Without Using a Temp Variable
  25. Java Program to Find the Second Largest Number in an Array
  26. Java Program to Check if a Given Number is Perfect Square
  27. Java Program to Find the Fibonacci Series
  28. Java Program to Check if a Number is Positive or Negative

Comments