We are uploading our contents, please refresh app.
Java Premium

Write a Java Program to Print the Armstrong numbers from 0 to 999

What is Armstrong Number ?

In an Armstrong Number, the sum of power of individual digits is equal to number itself.


In other words the following equation will hold true

xy..z = xn + yn+.....+ zn 
n is number of digits in number

For example this is a 3 digit Armstrong number

370 = 33 + 73 + 03
    = 27 + 343 + 0
    = 370

Examples of Armstrong Numbers

0, 1, 4, 5, 9, 153, 371, 407, 8208, etc
Program:
public class Armstrong_Range
{
public static void main(String[] args)
{
        int tempNumber, digit, digitCubeSum;
       for (int inputArmstrongNumber = 0; inputArmstrongNumber < 1000; inputArmstrongNumber++)
       {
            tempNumber = inputArmstrongNumber;
            digitCubeSum = 0;
            while (tempNumber != 0)
            {
                /* On each iteration, remainder is powered by thetempNumber of digits n
                 */
                 digit = tempNumber % 10;
                 //sum of cubes of each digits is equal to thetempNumber itself
                digitCubeSum = digitCubeSum + digit * digit * digit;
                 tempNumber /= 10;
            }
             //check giventempNumber and digitCubeSum is equal to or not
            if (digitCubeSum == inputArmstrongNumber)
                System.out.println(inputArmstrongNumber + " is an Armstrong Number");
         }
     }
 }

Output:
0 is an Armstrong Number
1 is an Armstrong Number
153 is an Armstrong Number
370 is an Armstrong Number
371 is an Armstrong Number
407 is an Armstrong Number

Online user

Download our android app Download App Submit your query
x

Get Updates On

Java & Premium Programs

Java Solved & Questions

Java Coding easy to use

Proper Coding

Straight Into Your INBOX!

We are going to send you our resources for free. To collect your copy at first, join our mailing list. We are promising not to bother you by sending useless information. So don't miss any updates, stay connected!