How to Write Java Program to Print Armstrong numbers from 0 to 999




How to Write Java Program to Print Armstrong numbers from 0 to 999

Java is an object oriented language and some concepts may be new. Take breaks when needed, and go over the examples as many times as needed.

Armstrong Number in Java: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

//ChecktempNumber is Armstrong or not using while loop
package com.guru99;
 
public class ArmstrongNumber {
 
    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

 



Author Biography.

Lokesh Gupta
Lokesh Gupta

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working in the information technology and services industry. I enjoy solving complex problems within budget and deadlines putting my skills on PHP, MySQL, Python, Codeigniter, Yii2, Laravel, AngularJS, ReactJS, NodeJS to best use. Through Knowledge of UML & visual modeling, application architecture design & business process modeling. Successfully delivered various projects, based on different technologies across the globe.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforJobs is an advanced hiring platform based on artificial intelligence, enabling recruiters to hire top talent effortlessly.

CrowdforJobs

CrowdforApps brings to you the well researched list of the most successful and finest App development companies, Web software developers.

CrowdforApps

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.

CrowdforGeeks

CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.

CrowdforThink
CFT

News & Blogs

1f00cec786cffaea25f9f74e5936755b.png

How to reverse a number using for loop in Java

The program will prompt user to input the number and then it will reverse the same number using l...

f5bc2fea6240f198c0119d6fe4a92e7c.png

How to reverse a number using while loop in Java

The program will prompt user to input the number and then it will reverse the same number using w...

dcc7d9d20769f5894731297e6e4aa8bd.png

How to reverse a number using recursion in Java

import java.util.Scanner; class RecursionReverseDemo { //A method for reverse public stati...

Top Authors

Hey, I am Suraj - a full-time blogger and a social media expert currently working on the Growth H...

Suraj Kumar

With good communication and writing skiils, Astha Sharma is a full-time content writer working wi...

Astha Sharma

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working i...

Lokesh Gupta

Aditya Sehgal is a recognised financial adviser, tech and gadget writer and blogger. Still he has...

Aditya Sehgal
CFT

Our Client Says

WhatsApp Chat with Our Support Team