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.
public class Armstrong
{
public static void main(String[] args)
{
int n, count = 0, a, b, c, sum = 0;
System.out.print("Armstrong numbers from 1 to 1000:");
for(int i = 1; i <= 1000; i++)
{
n = i;
while(n > 0)
{
b = n % 10;
sum = sum + (b * b * b);
n = n / 10;
}
if(sum == i)
{
System.out.print(i+" ");
}
sum = 0;
}
}
}
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
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check Java Books
- Apply for Java Internship
- Check Programming Books