Write a Java Program to Check given number is Armstrong or Not ? | StudyEcart | CODE007

What is an Armstrong Number?

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 in the number itself. In other words, if we have a number with 'n' digits, then the number is an Armstrong number if the sum of its digits, each raised to the power of 'n', is equal to the original number.


Examples of Armstrong Numbers:

Let's take a few examples to illustrate Armstrong's numbers:


1. **153**: This is a 3-digit number. The sum of its digits raised to the power of 3 is 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153, which is the original number. Hence, 153 is an Armstrong number.


2. **370**: Similarly, for this 3-digit number, the sum of its digits raised to the power of 3 is 3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370, which is the original number. Thus, 370 is also an Armstrong number.


3. **1634**: For this 4-digit number, the sum of its digits raised to the power of 4 is 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634, matching the original number. Hence, 1634 is an Armstrong number.


Detecting Armstrong Numbers in Java:

Now, let's delve into how we can write a Java program to determine whether a given number is an Armstrong number or not. Below is a clear and concise implementation:


   
import java.util.Scanner;

public class ArmstrongNumberDetector {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        scanner.close();

        int originalNumber = number;
        int sum = 0;
        int numberOfDigits = String.valueOf(number).length();

        while (number > 0) {
            int digit = number % 10;
            sum += Math.pow(digit, numberOfDigits);
            number /= 10;
        }

        if (sum == originalNumber) {
            System.out.println(originalNumber + " is an Armstrong number.");
        } else {
            System.out.println(originalNumber + " is not an Armstrong number.");
        }
    }
}

(code-box)

Explanation of the Java Program:

1. We take input from the user using the Scanner class.

2. We store the original number and calculate the number of digits in it.

3. We then iterate through each digit of the number, raise it to the power of the number of digits, and add it to the sum.

4. Finally, we compare the sum with the original number to determine whether it's an Armstrong number or not.

Conclusion:

Armstrong numbers are an interesting concept in mathematics that can be easily detected using programming. By understanding the relationship between digits and their powers, we can create efficient algorithms to identify these special numbers. The provided Java program offers a practical way to detect Armstrong numbers and showcases the application of mathematics in programming.

🚀 Elevate Your Career with Studyecart! 📚📊

🔥 Get instant job notifications and ace interviews with Studyecart. 📌

Download now

#StudyecartApp #CareerBoost

Post a Comment

0 Comments