Java Program to Check Armstrong Number

«
»

Armstrong Number in Java: Armstrong Number is an integer such that the sum of the cubes of its digits is equal to the number itself. Armstrong numbers are 0, 1, 153, 370, 371, 407, etc.

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.

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
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.

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 an input and store it in the variable “n”.
2. Use modulos and division operations along with loops to find the armstrong number.
3. Use if else statements to get the output.

Time Complexity: O(log(n))
O(log(n)) + O(log(n)) = O(log(n))
The above program for checking whether a number is Armstrong or not has a time complexity of O(log(n)) as both the while loop runs for log(n) times because at each iteration the number is divided by 10, where n is the number given as input.

Space Complexity: O(1)
space complexity of the armstrong program is O(1) as no extra variable is taken to store the value. All the variables initialized takes constant O(1) space.

Program Output
 
Enter a number: 371
371 is an Armstrong number
 
Enter a number: 150
150 is not an Armstrong number

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement

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.