Java Program to check Armstrong Number
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
public class Armstrong {
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 );
}
int order( int x)
{
int n = 0 ;
while (x != 0 ) {
n++;
x = x / 10 ;
}
return n;
}
boolean isArmstrong( int x)
{
int n = order(x);
int temp = x, sum = 0 ;
while (temp != 0 ) {
int r = temp % 10 ;
sum = sum + power(r, n);
temp = temp / 10 ;
}
return (sum == x);
}
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));
}
}
|
Output
153 : 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
import java.io.*;
class Armstrong {
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 );
}
int cal( int x, int dig)
{
if (x == 0 )
return 0 ;
int num = x % 10 ;
return cal(x / 10 , dig) + power(num, dig);
}
int cal_digits( int x)
{
int dig = 0 ;
while (x != 0 ) {
dig++;
x = x / 10 ;
}
return dig;
}
public boolean isArmstrong( int x)
{
int dig = cal_digits(x);
if (dig <= 1 )
return true ;
int res = cal(x, dig);
return res == x;
}
}
class GFG {
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));
}
}
|
Output
153 : true
1253 : false
The Complexity of the method above:
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
28 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...