Programs

# What is the Armstrong Number in Java? It’s Prerequisites and Algorithm

## What is the Armstrong Number?

A given number x is called an Armstrong number of order n if its equal to sum of its digits raised to power n, i.e:

abc= pow(a,n) + pow(b,n) + pow(c,n)

A positive integer of n digits is an Armstrong number (of order 3) if it is equal to the sum of cubes of its digits. For example:

Input: 153

Output: Yes

1*1*1 + 5*5*5 + 3*3*3 = 153

Thus, 153 is an Armstrong number

Input: 121

Output: No

1*1*1 + 2*2*2 + 1*1*1 = 10

Thus, 120 is not a Armstrong number.

## Prerequisites and Algorithm

In this article, we will learn to check whether an input number is an Armstrong number or not. For this, we will be using a while loop and for loop in Java. To understand this program, we need knowledge of the following topics in Java:

• Â  Java if-else statement
• Â  Java for Loop
• Â  Java while and do-while loop

Algorithm:

1. Â  Â  Take an integer variable x
2. Â  Â  Value is assigned to the variable x
3. Â  Â  The digits of the value are split
4. Â  Â  The cube value of each digit is found
5. Â  Â  Add the values of all the cubes
6. Â  Â  The output is saved to sum variable
7. Â  Â  If sum=X, print Armstrong number

If sum != X, print not Armstrong number

## Program to Check Armstrong Number in Java

1. Armstrong number in Java of order 3

Code:

public class Armstrong{Â

Â Â Â Â Â Â Â Â Â Â  Â  public static void main(String[] args)Â  {Â

Â Â Â Â Â Â Â Â Â Â  Â  Â  int x=0,a,temp;Â

Â Â Â Â Â Â Â Â Â Â  Â  Â  int n=153;//It is the number to check ArmstrongÂ

Â Â Â Â Â Â Â Â Â Â  Â  Â  temp=n;Â

Â Â Â Â Â Â Â Â Â Â  Â  Â  while(n>0)Â

Â Â Â Â Â Â Â Â Â Â  Â  Â  {Â

Â Â Â Â Â Â Â Â Â Â  Â  Â  a=n%10;Â

Â Â Â Â Â Â Â Â Â Â  Â  Â  n=n/10;Â

Â Â Â Â Â Â Â Â Â Â  Â  Â  x=x+(a*a*a);Â

Â Â Â Â Â Â Â Â Â Â  Â  Â  }Â

Â Â Â Â Â Â Â Â Â Â  Â  Â  if(temp==x)Â

Â Â Â Â Â Â Â Â Â Â  Â  Â  System.out.println(“Armstrong number”);Â Â

Â Â Â Â Â Â Â Â Â Â  Â  Â  elseÂ

Â Â Â Â Â Â Â Â Â Â  Â  Â  System.out.println(“Not Armstrong number”);Â Â

Â Â Â Â Â Â Â Â Â Â  Â  }Â

Â Â Â Â Â Â Â Â Â Â  }

Â Output:

Explanation

First, a given number Nâ€™s value is stored in another integer variable, x. This is done as we need to compare the values of the original number and the final number at the end. A while loop is used to lop through the number N until it is equal to 0. With every iteration, the last digit of N is stored in a.

Then the result is computed by cubing a or using Math. pow() and adding it to x. When the last digit is removed from N, it is divided by 10. Finally =, temp and X are compared. If they are equal, itâ€™s an Armstrong number. Otherwise, it isnâ€™t.

2. Armstrong number in Java of order n

Code:

public class Armstrong

{

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  /* Function to calculate a raised to the

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  power b */

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  int power(int a, long b)

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  if( b == 0)

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  return 1;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  if (b%2 == 0)

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  return power(a, b/2)*power(a, b/2);

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  return a*power(a, b/2)*power(a, b/2);

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }

Â

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  /* Function to calculate order of the number */

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  int order(int a)

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  int n = 0;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  while (a != 0)

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  n++;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  a = a/10;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  return n;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }

Â

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // Function to check whether the given number is Armstrong number or not

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  boolean Armstrong (int a)

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // Calling the order function

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  int n = order(a);

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  int temp=a, sum=0;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  while (temp!=0)

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  int r = temp%10;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  sum = sum + power(r,n);

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  temp = temp/10;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }

Â

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // If it satisfies Armstrong condition

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  return (sum == a);

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // Driver Program

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  public static void main(String[] args)

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  Armstrong obj = new Armstrong();

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  int a = 153;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  System.out.println(obj.isArmstrong(a));

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  a = 1276;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  System.out.println(obj.isArmstrong(a));

Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }

}

Output:

Explanation

In this program, instead of a while loop, we have used recursion. The first function is used to calculate a number a raised to power b. Math.pow(a,b) can also be used for the same. The second function checks the order of the number, like, 153 is of order 3. Finally, a Boolean function Armstrong is used to return if the number is Armstrong or not. Then an object of the Armstrong class is used to call the functions.

## Java Program to Print Armstrong Numbers in the Given Range

Code:

public class Armstrong

{

Â Â Â Â Â Â Â Â Â Â  public static void main(String[] arg)

Â Â Â Â Â Â Â Â Â Â  {

Â Â Â Â Â Â Â Â Â Â  int i=0,x;

Â Â Â Â Â Â Â Â Â Â  System.out.println(“Armstrong numbers between 0 to 999”);

Â Â Â Â Â Â Â Â Â Â  while(i<1000)

Â Â Â Â Â Â Â Â Â Â  {

Â Â Â Â Â Â Â Â Â Â  x=armstrongOrNot(i);

Â Â Â Â Â Â Â Â Â Â  if(x==i)

Â Â Â Â Â Â Â Â Â Â  System.out.println(i);

Â Â Â Â Â Â Â Â Â Â  i++;

Â Â Â Â Â Â Â Â Â Â  }

Â Â Â Â Â Â Â Â Â Â  }

static int armstrongOrNot(int n)

{

Â Â Â Â Â Â Â Â Â Â  int c,a=0;

Â Â Â Â Â Â Â Â Â Â  while(n!=0)

Â Â Â Â Â Â Â Â Â Â  {

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  c=n%10;

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  a=a+(c*c*c);

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  n/=10 ;

Â Â Â Â Â Â Â Â Â Â  }

Â Â Â Â Â Â Â Â Â Â  return a;

}

}

Output:

Explanation: In this program, each number between the given interval low and high, are checked. Post each check, the sum and number of digits are restored to 0.

## Conclusion

Â In this article, we covered an Armstrong number in java in detail. We saw how to check if a number is an Armstrong number of order n and order 3. We also saw how you could get all the Armstrong numbers between a lower and upper bound.

If you wish to improve your Java skills, you need to get your hands on these java projects. If youâ€™re interested to learn more about Java, full-stack development, check out upGrad & IIIT-Bâ€™s Executive PG Program in Full-stack Software DevelopmentÂ which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

## What is the logic behind the Armstrong number?

The Armstrong number is defined as a number in any number base that produces the sum of the same number when each one of the digits is raised to the power of the number's digits. The Armstrong number characteristic holds true in every number system. When it comes to practicing computer programming for applications, the Armstrong number program is considered one of the most fundamental. The logic behind the Amstrong number is that when built, the computer programmer understands different syntaxes and problem solving skills; hence, the significance.

## What is Java programming?

Java is an Object-Oriented programming language that has been one of the most popular for many years. It is not regarded as true object-oriented, though, because it supports basic data types such as int, char, and so on. First, the Java program is written into byte code. Then, independent of the underlying architecture, the byte code runs on the Java Virtual Machine (JVM). The syntax of Java is similar to that of C/C++. However, Java can not provide low-level coding languages such as pointers. In addition, Java programs are always expressed as classes and objects. Java is utilized in a wide range of applications, including mobile apps, desktop apps, web apps, client-server apps, corporate apps, and more.

## How is Java different from Python and C++?

Java code is more manageable when compared to C++ since it does not enable many things that, if used wrong, might lead to inadequate/inefficient programming. In Java, references to non-primitive are always used. As a result, unlike C++, we can't provide big objects to functions in Java; instead, references are passed. Bad memory access is also impossible due to the lack of pointers. Java falls between C++ and Python when compared to Python. Java applications are often quicker than Python equivalents but slower than C++ programs. Java performs static type checking, similar to C++, whereas Python does not.

## Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT