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

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.



News & Blogs

b155c4808d155c5ed54a2d70f917f6ea.png

An Introduction to Angular: The Remarkable JS F...

Javascript frameworks have nowadays become the most popular and most-used web app development sol...

0752431b16084e2bc68868f7908c94a1.png

Develop RESTful API using Node JS, Express JS

REST (Representational State Transfer) is web standards based architecture and uses HTTP Pro...

6f5026c52c20b8aa8e6452b8934aa46b.png

How to Build a Real-time Chat App With NodeJS, ...

In this tutorial, we’ll be building a real-time chat application with NodeJS, Express, Sock...