Java Program to Check Armstrong Number3 min read

In this tutorial, we will write a Java Program to check Armstrong number. We will write two different programs to check the armstrong number in java.

1. What is an Armstrong number?
2. Java program to check the Armstrong Number(for any digit number) using a while and for.
3. Java program to check the Armstrong Number(using Math.pow() method).


What is an Armstrong Number?

A number is said to be an Armstrong Number if even after the sum of its digits, where each digit is raised to the power of the number of digits is equal to the original number. For example 153, 371, 407, 9474, etc are Armstrong Numbers.

Follow the below calculation.

Armstrong Number in Java

Java program to check the Armstrong Number using while and for loops

Output: You can check for any digit Number.

Explanation:
First we create required variables(n, num, totalDigits, remainder, result). Get User Input and store it in n, then we need the number of digits present in that number and store it n totalDigits using code (totalDigits = String.valueOf(n).length();). After that, copy the entered number to num.

Now start the while loop checking num is not equal to zero. While stops as soon as num becomes zero.
After each iteration of the while loop, the last digit of num is stored in remainder for the second use. We initiate an int variable lastDigits = 1.

Now the for loop runs until the totalDigits less than i where is initiated as 0 and increase by 1 after each iteration. Inside for loop lastDigits is multiplied to remainder and the result is added to lastDigits.

After the end of for loop, the value of lastDigits is assigned to the result variable.
and at the end, we divide the num by 10 i.e num /= 10; which will remove the last digit from the number.
This step is continued until the num becomes 0.

At last, we will check with if-else statement whether the result is equal to the original number or not. If it is found equal,l then the entered number is an Armstrong Number.


Java program to check the Armstrong Number using Math.pow() method

Output: You can check for any digit Number.

Enter the Number: 370
370 is an Armstrong number

Explanation: In the above program we use the inbuilt method (Math.pow()) which makes it easy for us to calculate the raised power to the number.

The process is the same as explained above, only the difference is instead of for loop we use the method(Math.pow()) as result += Math.pow(remainder, power);.

If we were to calculate for the only three-digit number then we can simply replace the code:
result += Math.pow(remainder, power); with
result = result + remainder*remainder*remainder;


MORE

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More