Java Program to Check Armstrong Number

An Armstrong Number in Java is a unique integer where the sum of the cubes of its individual digits equals the number itself. Examples of Armstrong numbers include 0, 1, 153, 370, 371, 407, and more.

How to Identify Armstrong Numbers?

Identifying Armstrong numbers involves a simple algorithm:

  1. Count the number of digits (n) in the given number.
  2. Extract each digit of the number.
  3. Raise each digit to the power of n.
  4. Sum all the results.
  5. Check if the sum is equal to the original number. If it is, the number is an Armstrong number.

Formula to calculate Armstrong Number:

wxyz = pow(w,n) + pow(x,n) + pow(y,n) + pow(z,n)

Example:

Let’s look at 371 as an example to understand why it’s an Armstrong number.

advertisement
advertisement
371 = 3*3*3 + 7*7*7 + 1*1*1 
    = 27 + 343 +  1
    = 371
Problem Description

Write a Java program to check if a given number is Armstrong. If the number is an Armstrong then display it is an Armstrong number else display it is not an Armstrong number.

Methods to Find Armstrong Numbers in Java:

There are several ways to find armstrong number in Java language. Let’s take a detailed look at all the approaches to find armstrong number in Java.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Method 1: Armstrong Number Program in Java using While Loop

In this approach, we utilize a Java program to check whether a given number is an Armstrong number. This method utilizes a while loop for the calculations.

Program/Source Code

Here is the source code of the Java Program to Check If a Given Number is ArmStrong Number. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Armstrong Number in Java using While Loop
 */
 
import java.util.Scanner;
public class ArmStrong 
{
    public static void main(String[] args) 
    {
        int n, count = 0, a, b, c, sum = 0;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter a number:");
        n = s.nextInt();
        a = n;
        c = n;
        while(a > 0)
        {
            a = a / 10;
            count++;
        }
        while(n > 0)
        {
            b = n % 10;
            sum = (int) (sum+Math.pow(b, count));
            n = n / 10;
        }
        if(sum == c)
        {
            System.out.println(c+ " is an Armstrong number");
        }
        else
        {
            System.out.println(c+ " is not an Armstrong number");
        }    
    }
}
Program Explanation

1. Take the number as input and store it in the variable “n“.
2. Initialize variables “count”, “a”, “b”, “c”, and “sum” to keep track of the calculation.
3. Use a while loop to count the number of digits in the input number and store it in the “count” variable.
4. Use another while loop to calculate the sum of the cubes of each digit in the input number and store it in the “sum” variable.
5. Check if the calculated sum is equal to the original number “c”.
6. If the sum is equal to “c”, print that it’s an Armstrong number; otherwise, print that it’s not.

Time Complexity: O(log(n))
The program has a time complexity of O(log(n)) because both while loops run for log(n) iterations. In each iteration, the number is divided by 10 until it becomes 0.

advertisement

Space Complexity: O(1)
The space complexity of the program is O(1) because it uses a fixed number of variables, and no additional space grows with the input size.

Runtime Test Cases

Testcase 1: In this case, we enter the number “371” as input to check whether it is a armstrong number or not.

 
Enter a number: 371
371 is an Armstrong number

Testcase 2: In this case, we enter the number “150” as input to check whether it is a prime number or not.

Enter a number: 150
150 is not an Armstrong number

advertisement
Method 2: Armstrong Number Program in Java using For Loop

This Java program checks whether a given number is an Armstrong number using for loops, calculating the number of digits and the sum of cubes of digits.

Program/Source Code

Here is the source code of the Java Program to Check If a Given Number is Armstrong Number using for loop. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Armstrong Number in Java using For Loop
 */
 
import java.util.Scanner;
 
public class ArmStrong
{
    public static void main(String[] args)
    {
        int n, count = 0, a, b, c, sum = 0;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter a number:");
        n = s.nextInt();
        a = n;
        c = n;
 
        // Calculate the number of digits in 'n' using a for loop
        for (; a > 0; a /= 10)
        {
            count++;
        }
 
        // Reset 'a' and 'sum' for the following loop
        a = n;
        sum = 0;
 
        // Calculate the sum of cubes of digits using a for loop
        for (; n > 0; n /= 10)
        {
            b = n % 10;
            sum += Math.pow(b, count);
        }
 
        if (sum == c)
        {
            System.out.println(c + " is an Armstrong number");
        }
        else
        {
            System.out.println(c + " is not an Armstrong number");
        }
    }
}
Program Explanation

1. First, we take an integer input “n” from the user.
2. Then, we initialize variables to perform calculations.
3. Using a loop, we count the number of digits in “n”.
4. Another loop calculates the sum of the cubes of each digit.
5. We check if this calculated sum is equal to the original number.
6. Finally, we display the result, indicating whether it’s an Armstrong number or not.

Complexity Analysis:

The Java program using for loops to check for Armstrong numbers has a time complexity of O(log(n)) as both loops run for log(n) iterations. Its space complexity is O(1) since it uses a fixed number of variables, regardless of the input size.

Runtime Test Cases

Testcase 1: In this case, we enter the number “153” as input to check whether it is a armstrong number or not.

 
Enter a number: 153
153 is an Armstrong number

Testcase 2: In this case, we enter the number “442” as input to check whether it is a prime number or not.

Enter a number: 442
442 is not an Armstrong number

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.