Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

How to Check Given Number is Armstrong or not

Last Updated on May 9, 2023 by Prepbytes

In this article, We shall learn what a Java Armstrong Number is. The method for determining if a number is Armstrong or not will be shown, along with its code.

We recommend the following topics of Java for a better understanding of the program.

  • The basic syntax of Java.
  • Loops in Java.
  • Decision statement in Java.

What is an Armstrong Number in Java?

In Java, a number is said to be an Armstrong number if the sum of its own digits raised to a power of digits results in the number itself. For instance, the Armstrong numbers 0, 1, 153, 370, 371, 407, 1634, 8208, and 9474 are both three- and four-digit Armstrong numbers. They are numerous. Consider the number 407, which may be stated as follows because it has three digits:

(407) —> (4 * 4 * 4)  +  (0 * 0 * 0)  +  (7 * 7 * 7)
           64 + 0 + 343
        => 407

As 407 is a sum of the cube of its digits, therefore it is an Armstrong number.

Implementation of Armstrong Number Program in Java

First, we have to declare the class AmstrongNum.Then declare the variables. Create an object of the scanner class as sc and read the number from the user into the variable num. Assign originalNum=num. Find the sum of cubes of digits of num by using a while loop with condition num != 0,

  • Take each digit by finding the remainder of num divided by 10, digit=num
  • Find the cube of the digit using the pow() method of Java, add it with cube_sum
  • To find the next digit we’ll assign the num variable = num/10.

After exiting from the loop, check if cube_sum is the same as originalNum, if yes then display the number is Armstrong number, else display the number as not Armstrong number using the system.out.println().

Algorithm of Armstrong Number Program

  1. Declare the class ArmstrongNum with a public modifier.
  2. To start the program open the main(), Java program execution starts with the main()
  3. Declare the integer variables originalNum, digit, cubeSum, num
  4. Assign cubeSum = 0
  5. Create an object of the scanner class
  6. Read the number into the variable num
  7. Assign originalNum=num
  8. By using a while loop with the condition originalNum !=0, do steps 9,10
  9. Find the digit, the cube of that digit, and the sum of cubes into cubeSum
  10. Then to find the next digit divide the num by 10
  11. After processing the loop, check if cubeSum is equal to originalNum
    • if true then display the number as Armstrong number
    • else display the number is not Armstrong.
import java.util.Scanner;

class ArmstrongNum {

    public static void main(String[] args) {

        int originalNum, digit, cubeSum = 0,num;
        
        System.out.println("Enter the number:");
        Scanner sc = new Scanner(System. in );
        num = sc.nextInt();
        originalNum = num;

        while (num!= 0)
        {
            digit = num % 10;
            cubeSum += Math.pow(digit, 3);
            num /= 10;
        }

        if(cubeSum == originalNum)
            System.out.println(originalNum+ " is an Armstrong number");
        else
            System.out.println(originalNum+ " is not an Armstrong number");
    }
}

Dry Running The Armstrong Number Code

Let input is equal to 153
variable values before for loop
num=153; check=153; Armstrong=0; result=0;
values of the variable in for loop are line by line

for i=1
Armstrong=3;
num=15;
Armstrong=333=27
result=0+27=27;
for loop condition check: num is not equal to zero loops will run again

for i=2
Armstrong=5;
num=1;
Armstrong=555=125
result=27+125=152;
for loop condition: num is not equal to zero loops will run again

for i=3
Armstrong=1;
num=0;
Armstrong=111=1;
result=152+1=153;
In for loop condition, the num is EQUAL TO ZERO therefore, the loop will run again

The loop will break and if else condition will be checked as or result=153 and check=153
if the condition will true program will show the output
153 is an Armstrong Number

Finding the Armstrong Number Using Static Method Between 100 and 999

Given below is the first sample program using the static method with sample output as well. Here we used the method ‘ Static ‘ and took numbers between 100 to 999. Once you have done the execution, it automatically displays the Armstrong numbers between 100 and 999. Check out the sample output.

import java.util.Scanner;
class ArmstrongWhile
{
	public static void main(String[] arg)
	{
	int i=100,arm;
	System.out.println("Armstrong numbers between 100 to 999");
	while(i<1000)
	{
	arm=armstrongOrNot(i);
	if(arm==i)
	System.out.println(i);
	i++;
	}
	}
static int armstrongOrNot(int num)
{
	int x,a=0;
	while(num!=0)
	{
		x=num%10;
		a=a+(x*x*x);
		num/=10 ;
	}
	return a;
}
}

Checking Armstrong Number Using While Loop ( 1 to 500 )

There you go another method using a while loop. Java, a while loop is nothing but executing a set of statements repeatedly as long as the condition is true – here is the complete guide on a while loop in Java with examples. For this case, we have taken the example of numbers from 1 to 500. Upon execution, you will get to know what Armstrong numbers are. Check out the output below so that you will get an idea.

class ArmstrongWhile
{
	public static void main(String[] arg)
	{
	int i=1,a,arm,n,temp;
	System.out.println("Armstrong numbers between 1 to 500 are");
	while(i<500)
	{
	n=i;
	arm=0;
	while(n>0)
	{
		a=n%10;
		arm=arm+(a*a*a);
		n=n/10;
	}
	if(arm==i)
		System.out.println(i);
	i++;
	}
	}
}

Find the Armstrong Number Using Recursion ( Between 1 to 1000 )

Another method using recursion: A function that calls itself is called recursion. Here is the complete code with the sample program and the output of the program. Here we have taken the example of numbers from 1 to 1000. Check it out.

import java.util.Scanner;
class ArmstrongRecursion
{
	int x;
	int findArmstrong(int n,int a)
	{
	if(n!=0)
	{
		x=n%10;
		a=a+(x*x*x);
		n/=10 ;
		return findArmstrong(n,a);
	}
	return a;
	}
	public static void main(String[] arg)
	{
	ArmstrongRecursion A=new ArmstrongRecursion();
	int arm;
	System.out.println("Armstrong numbers between 1 to 1000");
	for(int num=1;num<500;num++)
	{
	arm=A.findArmstrong(num,0);
	if(arm==num)
	      System.out.println(num);
	}
	}
}

Checking Armstrong Number Using for loop ( 100 to 500 )

Another sample program to print Armstrong numbers using a for a loop. Look out for the program given below.

import java.util.Scanner;
class Armstrong
{
	public static void main(String[] arg)
	{
	int a,arm=0,n,temp;
	Scanner sc=new Scanner(System.in);
	System.out.println("Enter a number");
	n=sc.nextInt();
	temp=n;
	for( ;n!=0;n/=10 )
	{
		a=n%10;
		arm=arm+(a*a*a);
	}
	if(arm==temp)
	System.out.println(temp+" is a armstrong number ");
	else
	System.out.println(temp+" is not a armstrong number ");
        }
}

Conclusion
We have discussed the Armstrong number program in Java with its algorithm and implementation also with the dry run. Apart from it, we have also discussed different methods (static method, for loop, while loop and recursion) to check or find Armstrong number in java.

Armstrong Number In Java – FAQs

Q1. Which companies asked for Armstrong Number in java in an interview?
Ans. Companies like Gemini Solution, TCS, Infosys, and Unthinkable Solutions had recently asked for Armstrong Number in their interview round.

Q2. What is the Armstrong Number in Java?
Ans. The Armstrong number is defined as three digits in an integer, where the sum of the cubes of its digits is equal to the number itself.

Q3. Where is Java mostly used?
Ans. Today developers use Java to construct applications, data centers, game consoles, scientific supercomputers, cell phones, and other devices. In today’s world, Java is the third most popular programming language, after Python and C according to the TIOBE index.

Q4 . What is the Armstrong number’s time complexity?
Ans. Since there are log N digits in a number N and we assume that power takes a constant amount of time, the complexity is O(log N).

Q5.Are there infinite Armstrong numbers?
Ans. As a result, each base has a fixed number of Armstrong numbers. Consider a three-digit number in base two, where b is 2 and n is 3, as an illustration.

Leave a Reply

Your email address will not be published. Required fields are marked *