Java Program to check Armstrong Number
Last Updated :
28 Feb, 2024
Given a number x. Write a Java Program to determine whether the given number is Armstrong's number or not. In this article, we will learn how to check Armstrong Numbers in Java.
What is Armstrong's Number?
In a mathematical number system, the Armstrong number is the number in any given number base, which makes the total of the same number when each of its digits is raised to the power of the total number of digits in the number. In simple words, we can say that a positive integer of n digits is called an Armstrong number of order n (order is the total number of digits present in a number) if,Â
abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
Examples of Java Armstrong Number

Note: There are some examples of Armstrong Number in Java are: 1 , 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, etc.
Program for Java Armstrong Number
There are not many known methods to find Amstrong Number few of them are mentioned below:
- Using Loops and Basic Operations
- Recursive Method
1. Java Armstrong Number Using Basic Operations
Below is the implementation of the above method:
Java
// Java program to determine whether the
// Number is Armstrong number or not
public class Armstrong {
// Function to calculate x raised to the
// power y
int power(int x, long y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}
// Function to calculate order of the number
int order(int x)
{
int n = 0;
while (x != 0) {
n++;
x = x / 10;
}
return n;
}
// Function to check whether the given number is
// Armstrong number or not
boolean isArmstrong(int x)
{
// Calling order function
int n = order(x);
int temp = x, sum = 0;
while (temp != 0) {
int r = temp % 10;
sum = sum + power(r, n);
temp = temp / 10;
}
// If satisfies Armstrong condition
return (sum == x);
}
// Driver Program
public static void main(String[] args)
{
Armstrong ob = new Armstrong();
int x = 153;
System.out.println(x + " : " + ob.isArmstrong(x));
x = 1253;
System.out.println(x + " : " + ob.isArmstrong(x));
}
}
Output153 : true
1253 : false
The complexity of the above method
Time Complexity: O(log10(n))
Auxiliary Space: O(1)
2. Recusive Program for Java Armstrong Number
Below is the implementation of the above method:
Java
// Java Program to implement Recursive
// Method for Checking Armstrong Number
import java.io.*;
// Armstrong Class
class Armstrong {
// Function to Calculate power
// Of a Number
int power(int x, long y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}
// Function to Perform all the
// Calculation
// (Recursive Method)
int cal(int x, int dig)
{
if (x == 0)
return 0;
int num = x % 10;
return cal(x / 10, dig) + power(num, dig);
}
// Functions to Calculate Number
// of Digits in a Number
int cal_digits(int x)
{
int dig = 0;
while (x != 0) {
dig++;
x = x / 10;
}
return dig;
}
// Function to check if a
// Number is Armstrong or Not
public boolean isArmstrong(int x)
{
int dig = cal_digits(x);
if (dig <= 1)
return true;
int res = cal(x, dig);
return res == x;
}
}
// Driver Class
class GFG {
// main function
public static void main(String[] args)
{
Armstrong ob = new Armstrong();
int x = 153;
System.out.println(x + " : " + ob.isArmstrong(x));
x = 1253;
System.out.println(x + " : " + ob.isArmstrong(x));
}
}
Output153 : true
1253 : false
The Complexity of the method above:
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Java Program to Check Armstrong Number between Two Integers A positive integer with digits p, q, r, sâ¦, is known as an Armstrong number of order n if the following condition is fulfilled. pqrs... = pn + qn + rn + sn +....Example: 370 = 3*3*3 + 7*7*7 + 0 = 27 + 343 + 0 = 370Therefore, 370 is an Armstrong number. Examples: Input : 100 200 Output :153 Explanati
2 min read
Java Program to Check For a Valid Mobile Number In Java, validating mobile numbers is usually done by using Regular Expressions (Regex) that define patterns for matching strings. Mobile number validation can be implemented for both local (Indian) and global phone numbers.Example: Now, let's take a basic example, to check if a mobile number is val
3 min read
Java Program to Check If a Number is Neon Number or Not A neon number is a number where the sum of digits of the square of the number is equal to the number. The task is to check and print neon numbers in a range. Illustration: Case 1: Input : 9 Output : Given number 9 is Neon number Explanation : square of 9=9*9=81; sum of digit of square : 8+1=9(which
3 min read
Prime Number Program in Java A prime number is a natural number greater than 1, divisible only by 1 and itself. Examples include 2, 3, 5, 7, and 11. These numbers have no other factors besides themselves and one.In this article, we will learn how to write a prime number program in Java when the input given is a Positive number.
6 min read
Palindrome Number Program in Java A given number can be said to be palindromic if the reverse of the given number is the same as that of a given number. In this article, we will write a Program to check if a number is a Palindrome Number in Java.Example of Palindrome Number:Input : n = 121Output: Reverse of n = 121Palindrome : YesIn
7 min read