In Java, to check a given number is Armstrong or not we can use loop concepts such as while loop or for loop.
Armstrong numbers are positive n-digit numbers that are equal to the sum of their digits’ nth powers. Here n is the count of digits in input number. For example 153 is an Armstrong number.
1234… n = 1 ^ n + 2 ^ n + 3 ^ n + ………+ n ^ n
In this article, we will learn how to check Armstrong number through examples, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
Problem Statement
We have to write a java program to check a given number is Armstrong or not. The program should take a integer number and check if this number is Armstrong number or not. This action will be done in our program. Let’s see an example,
Example 1:
For input 153, Output should be “Armstrong number”.
Explanation:
Input integer number = 153
Total digit count in input number = 3
Calculating 1^3 + 5 ^ 3 + 3 ^ 3 = 1 + 125 + 27 = 153
So input number(153) is equal to sum of their digits’ nth powers (153), here n is 3. We can say 153 is Armstrong number.
Example 2:
For input 1322, output should be “Not an Armstrong number”.
Explanation:
Input integer number = 1322
Total digit count in input number = 4
Calculating 1^4 + 3 ^ 4 + 2 ^ 4 + 2 ^ 4 = 1 + 81 + 16 + 16 = 114.
So input number 1322 is not equal to sum of their digits’ nth powers (114), here n is 4. We can say 153 is Armstrong number.
Logic to check a given number is Armstrong or not
- Take an input number (num) from the user.
- Initialize count to 0 to count the number of digits in num.
- Create a copy of num(int a =num;) to count the digits correctly.
- Use a loop (while or for loop) to count the number of digits (count) by repeatedly dividing a by 10 and incrementing count.
- Initialize sum to 0 to accumulate the powered values of digits.
- Use another loop (while or for loop) to extract each digit from num:
- Calculate the last digit using b=num%10.
- Compute b raised to the power of count using Math.pow(b, count), and add it to sum.
- Update num by integer division (num=num/10).
- After the loop completes, use an if-else statement to compare sum with the original number (num1):
- If sum equals num1, print that num1 is an Armstrong number.
- Otherwise, print that num1 is not an Armstrong number.
Java program to check a given number is Armstrong or not
Program 1: Using while loop
In the below program we are using while loop to check a given number is Armstrong or not in Java.
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter an Integer number");
int num = s.nextInt();
int count=0 , sum=0, a=num, b=num, num1=num;
while(a!=0) {
a=a/10;
count++;
}
while(num!=0) {
b=num%10;
sum = (int)(sum+Math.pow(b,count));
num=num/10;
}
System.out.println("sum is: "+sum);
if(sum==num1)
System.out.println(num1+" is an Armstrong number");
else
System.out.println(num1+" is not an Armstrong number");
s.close();
}
}
Output:
Enter an Integer number
407
sum is: 407
407 is an Armstrong number
Java program explanation to check a given number is Armstrong or not
- The above java program imports Scanner class from java.util for user input.
- Asks user to enter a number.
- Reads entered number and stores it in variable
num
. - Initializes
count
andsum
to 0 for tracking digits and their sum respectively. - Calculates number of digits (
count
) by repeatedly dividinga
by 10. - Iterates through each digit of
num
using a while loop. - Calculates each digit (
b
) by taking remainder ofnum
divided by 10. - Adds cube of each digit to
sum
. - Divides
num
by 10 to move to next digit. - Compares
sum
with original number (num1
). - Prints whether number is Armstrong or not.
- Closes Scanner object for releasing resources
Program 2: Using for loop
In the below program we are using for loop to check a given number is Armstrong or not.
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter an Integer number");
int num = s.nextInt();
int count=0 , sum=0, b=num, num1=num;
for(int a=num;a!=0;a/=10) {
count++;
}
for(;num!=0;num/=10) {
b=num%10;
sum = (int)(sum+Math.pow(b,count));
}
System.out.println("sum is: "+sum);
if(sum==num1)
System.out.println(num1+" is an Armstrong number");
else
System.out.println(num1+" is not an Armstrong number");
s.close();
}
}
Output:
Enter an Integer number
256
sum is: 349
256 is not an Armstrong number
Java program explanation to check a given number is Armstrong or not
- The above java program imports the Scanner class to enable user input.
- Asks the user to enter a number.
- Reads the entered number and stores it in
num
. - Initializes variables
count
andsum
. - Uses a for loop to calculate the number of digits in
num
and store it incount
. - Iterates through each digit of
num
using another for loop. - Calculates the sum of each digit raised to the power of the total number of digits (
count
). - Compares the calculated sum with the original number.
- Prints whether the number is an Armstrong number or not.
- Closes the Scanner object.
Conclusion
In this article, we have learnt how to check a given number is Armstrong number or not in Java. We have learnt the logic and moved on to writing the program. Hope this article helped you in the understanding of the program.
You can also check our another awesome tutorials:
- Java program to check character is vowel or consonant
- Print sum of digits of a number in Java
- Java program to print all prime numbers in a range
- Find all natural numbers in a given range in Java
Happy Coding!