Armstrong Number between 100-999 in Java


The Java program prints all the Armstrong numbers between 100 and 999. An Armstrong number is a number whose sum of the cubes of its digits is equal to the number itself.

The program starts by declaring four integer variables: digit1, digit2, digit3, and result. These variables are used to store the digits of a three-digit number and the result of the sum of the cubes of its digits, respectively.

Then, a for loop is used to iterate through all the three-digit numbers between 100 and 999. Inside the loop, the current number is stored in a temporary variable temp, and its three digits are extracted using the modulo and division operators. The digits are then used to calculate the sum of the cubes of the digits, which is stored in the result variable.

If the result is equal to the current number, then it is an Armstrong number and the program prints it to the console. Overall, the program looks correct and should output all the Armstrong numbers between 100 and 999.

Source Code

public class Armstrong_Numbers {
    //Write a program to find the armstrong number from 100-999
    public static void main(String[] args)
    {
        int digit1,digit2,digit3,result,temp;
        for(int number = 100; number <= 999; number++)
        {
            temp = number;
            digit3=temp%10;//3
            temp=temp/10;//12
 
            digit2=temp%10;//2
            temp=temp/10;//1
 
            digit1=temp%10;//1
            result=(digit1 * digit1 * digit1)+( digit2 * digit2 * digit2)+(digit3 * digit3 * digit3);
 
            if(number==result){
                System.out.println(number + " is armstrong Number");
            }
        }
    }
}
 

Output

153 is armstrong Number
370 is armstrong Number
371 is armstrong Number
407 is armstrong Number
To download raw file Click Here

Basic Programs