Armstrong Number In Java – Check whether the given number is Armstrong or not.

In this article we will understand what an Armstrong Number is.We will understand the algorithm to check if a number is Armstrong or not 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?

A number is thought of as an Armstrong number if the sum of its own digits raised to the power number of digits gives the number itself. For example, 0, 1, 153, 370, 371, 407 are three-digit Armstrong numbers and, 1634, 8208, 9474 are four-digit Armstrong numbers and there are many more.Let’s consider 407 since it is a three-digit number so it could be expressed as:

(407) —> (444) + (000) + (777)
64 + 0 + 343
=> 407

Since 407 can be expressed as a sum of the cube of its digits, hence it is an Armstrong number.

How to implement the java program to check whether a number is Amstrong or not?

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 digit using pow() method of Java, add it with cube_sum
  • To find the next digit assign num = 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 is not Armstrong number using system.out.println().

Algorithm

  1. Declare the class ArmstrongNum with a public modifier.
  2. Open the main() to start the program, 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 as sc
  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, 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 loop, check if cubeSum is equal to originalNum
    • if true then display the number is 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;
variable values in for loop 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 loop 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 loop will run again

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

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

Different Approaches to find the Armstrong Number in a given range.

Approach 1: Using static method Between 100 and 999
Here 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 are done with 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;
}
}

Approach 2: Using while Loop ( 1 to 500 )
There you go another method using a while loop. Java, while loop is nothing but executing a set of statements repeatedly as long as the condition is true – here is the complete guide on while loop in Java with examples. In 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++;
	}
	}
}

Approach 3: 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);
	}
	}
}

Approach 4: Using for loop ( 100 to 500 )
Another sample program to print Armstrong numbers using a for loop. Check out the program along with the sample output.

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

Armstrong Number In Java FAQs

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

  2. What is the Armstrong Number in Java?
    An Armstrong number of three digits is an integer, where the sum of the cubes of its digits is equal to the number itself.

  3. Where is Java mostly used?
    Developers use Java to construct applications in laptops, data centers, game consoles, scientific supercomputers, cell phones, and other devices. Java is the world’s third most popular programming language, after Python and C – according to the TIOBE index, which evaluates programming language popularity.

This article tried to discuss whether the given number is the Armstrong number or not in Java. Hope this blog helps you understand and solve the problem. To practice more problems you can check out MYCODE | Competitive Programming at PrepBytes.

Leave a Reply

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