Java Program to Print Armstrong Number between 1 to 1000

This is a Java Program to Print Armstrong Number from 1 to 1000. Armstrong Number is an integer such that the sum of the cubes of its digits is equal to the number itself.

We use the Modulo and division operation along with loops and if conditions to get the desired output.

Here is the source code of the Java Program to Print Armstrong Number from 1 to 1000. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. public class Armstrong
  2. {
  3.     public static void main(String[] args) 
  4.      {
  5.         int n, count = 0, a, b, c, sum = 0;
  6.         System.out.print("Armstrong numbers from 1 to 1000:");
  7.         for(int i = 1; i <= 1000; i++)
  8.         {
  9.             n = i;
  10.             while(n > 0)
  11.             {
  12.                 b = n % 10;
  13.                 sum = sum + (b * b * b);
  14.                 n = n / 10;
  15.             }
  16.             if(sum == i)
  17.             {
  18.                 System.out.print(i+" ");
  19.             }
  20.             sum = 0;
  21.         }
  22.     }
  23. }

Output:

$ javac Armstrong.java
$ java  Armstrong
 
Armstrong numbers from 1 to 1000:1 153 370 371 407

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.