Java

An Armstrong number (also known as a narcissistic number, plenary number, or pluperfect number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.

Understanding the Code:

1.   The code initializes variables originalNumber, remainder, and result. originalNumber stores the original value, remainder stores the remainder when dividing the number by 10 (i.e., the last digit), and result stores the sum of the digits raised to the power of the number of digits.

2.   Inside the while loop, the code iterates through each digit of the original number. It calculates the cube of each digit and adds it to the result.

3.   After processing all digits, the code checks if the result is equal to the original number. If they are equal, it indicates that the number is an Armstrong number.

4.   Depending on the result of the check, the code prints either "Armstrong Number" or "Not an Armstrong number" to indicate whether the input number is an Armstrong number or not.

public class ArmstrongNumber {
    public static void main(String[] args) {
       
        int originalNumber,remainder,result=0;


        originalNumber=154;
        int number=originalNumber;


        while(originalNumber!=0){
            remainder=originalNumber%10;
            result=result+(remainder*remainder*remainder);
            originalNumber=originalNumber/10;
           
        }


        if(result == number){
            System.out.println("Armstrong Number");
        }
        else{
            System.out.println("Not an Armstrong number");
        }


    }
}

 

Conclusion: In conclusion, it can be effectively determined whether a given number is an Armstrong number using the Java code that is provided. The code iterates through each digit of the number, computes the sum of the cubes of the digits, and compares it with the original number to determine the result. It does this by utilizing simple arithmetic operations and looping structures. Armstrong numbers are an interesting topic in mathematics, and this code provides a useful way to find them in Java programs.

 

Related Posts

Table Of Contents

;