Open In App

Java Program to check Armstrong Number

Improve
Improve
Like Article
Like
Save
Share
Report

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

Armstrong-Number-in-Java

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:

  1. Using Loops and Basic Operations
  2. 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));
    }
}


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




// 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));
    }
}


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
Previous
Next
Share your thoughts in the comments
Similar Reads