Java Program for Armstrong number

Java Program for Armstrong number
Reading Time: 7 minutes

Introduction:

The provided Java program is designed to determine whether a given number is an Armstrong number or not. 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 in the number. This program takes a user-inputted number and checks if it satisfies the Armstrong number condition.

Algorithm:

  1. Input:
    • A number is taken as input from the user using the Scanner
  2. Function isArmstrong(int num):
    • The isArmstrong function is defined to check if a given number is an Armstrong number.
    • Declare variables originalNumber, remainder, result, and n.
    • Initialize originalNumber with the input number to preserve the original value.
    • Use a while loop to count the number of digits in originalNumber and store the count in n.
    • Reset originalNumber to the input number.
    • Use another while loop to calculate the sum of each digit raised to the power of n and accumulate the result in the variable result.
    • Check if the calculated result is equal to the original number.
    • If the condition is met, the function returns true, indicating that the number is an Armstrong number. Otherwise, it returns false.
  3. Output:
    • Based on the result of the isArmstrong function, the program prints whether the input number is an Armstrong number or not.

Explanation:

  1. The program starts by taking a number as input from the user.
  2. The isArmstrong function is called with the input number to determine if it is an Armstrong number.
  3. Inside the isArmstrong function, the number of digits (n) is calculated, and then the sum of each digit raised to the power of n is computed.
  4. The program prints whether the input number is an Armstrong number or not based on the result of the isArmstrong

In summary, the program efficiently checks whether a given number is an Armstrong number using a well-defined algorithm that involves counting digits and performing the necessary calculations to verify the condition.

Code :

				
					import java.util.Scanner;
public class ArmstrongNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        if (isArmstrong(number)) {
            System.out.println(number + " is an Armstrong number.");
        } else {
            System.out.println(number + " is not an Armstrong number.");
        }
    }
    // Function to check if a number is an Armstrong number
    private static boolean isArmstrong(int num) {
        int originalNumber, remainder, result = 0, n = 0;
        originalNumber = num;
        // Count number of digits
        while (originalNumber != 0) {
            originalNumber /= 10;
            ++n;
        }
        originalNumber = num;
        // Calculate result
        while (originalNumber != 0) {
            remainder = originalNumber % 10;
            result += Math.pow(remainder, n);
            originalNumber /= 10;
        }
        // Check if num is Armstrong
        return result == num;
    }
}

				
			

Input :

				
					Enter a number: 69
				
			

Output :

				
					69 is not an Armstrong number.
				
			

This program takes a number as input and checks if it’s an Armstrong number or not. An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits.

FAQ’s :

  1. Q: Can you provide a simple Java program for checking Armstrong numbers?
    • A: Certainly! Here’s a basic Java program to determine if a given number is an Armstrong number.
  2. Q: How can I write a Java program to find Armstrong numbers?
    • A: It’s quite straightforward! Below is a Java program that identifies Armstrong numbers. Feel free to use and modify it.
  3. Q: What is the structure of a Java program for checking Armstrong numbers?
    • A: Here’s a standard Java program for determining Armstrong numbers. You can customize it based on your requirements.
  4. Q: Could you share a Java program of Armstrong number with explanation?
    • A: Absolutely! Here’s a Java program to detect Armstrong numbers along with comments explaining each step for better understanding.
  5. Q: Where can I find a reliable Java program for Armstrong number validation?
    • A: Right here! Below is a tested and reliable Java program to check if a given number is an Armstrong number. Copy it, use it, and explore!