Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconGeneralbreadcumb forward arrow iconWhat is the Armstrong Number in Java? It’s Prerequisites and Algorithm

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

Last updated:
8th Apr, 2021
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
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

Ads of upGrad blog

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:

 

Ads of upGrad blog

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.

If you are looking to learn more about Java and move up in your technical career, then explore free Java online course by India’s largest online higher education company, upGrad. Contact us for more details. 

Profile

Arjun Mathur

Blog Author
Arjun is Program marketing manager at UpGrad for the Software development program. Prior to UpGrad, he was a part of the French ride-sharing unicorn "BlaBlaCar" in India. He is a B.Tech in Computers Science from IIT Delhi and loves writing about technology.
Get Free Consultation

Select Coursecaret down icon
Selectcaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Popular MBA Course

Frequently Asked Questions (FAQs)

1What 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.

2What 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.

3How 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.

Explore Free Courses

Suggested Blogs

Top 18 Highest Paying Jobs in the World in 2023 [A Complete Guide]
909857
Summary In this article, you will learn about the top 18 highest-paying jobs in the world. And you will be able to get the answer to ‘Which job has t
Read More

by Nitin Gurmukhani

14 Sep 2023

Top 15 Highest Paid IT Jobs in India for Freshers &#038; Experienced [A Complete Guide]
901521
Summary: In this Article, you will learn top 15 highest paid IT jobs in India for freshers & Experienced. Top 15 Highest Paid IT Jobs Average
Read More

by upGrad

14 Sep 2023

Top 10 Highest Paying IT Certifications in India in 2023 [Astonishing]
65364
Summary: In this article, you will learn about the top 10 highest-paying IT certifications in India. Take a glimpse below. Business Intelligence (B
Read More

by Rahul Karthikeyan

13 Sep 2023

Top Career Options After 12th Science: What Course To Do After 12th Science
335997
Summary In this article, you will learn the Top Career Options After 12th Science. Take a glimpse at the below fields Medicine Engineering Business
Read More

by Kamal Jacob

13 Sep 2023

Salary of MCA Graduates: Based on Various Specializations &#038; Job Roles [2023]
899256
Are you an MCA student wondering what career options you have? Then you’ve come to the right place because this article will tell you the averag
Read More

by Sriram

12 Sep 2023

12 Best Career Options after M.Com: What to do After M.Com? [2023]
257720
Summary In this article, you will learn about the 12 Best Career Options after M.Com. Take a glimpse below. Enrol in an MBA Program Become a Chartere
Read More

by Rahul Karthikeyan

11 Sep 2023

Career Options in Commerce With Maths: 6 Top Courses To Select in 2023
59436
Are you commerce with maths student wondering what career you should choose? Then you’ve come to the right place. This article will discuss the
Read More

by Sriram

10 Sep 2023

21 Fun &amp; Interesting Career Options in India [2023]
28772
Are you wondering if there’s a career that matches your unique set of aspirations and interests? We are pretty sure there is one. This article w
Read More

by Sriram

10 Sep 2023

Salary of B.Com Graduates: Based on Specializations &#038; Job Roles [2023]
899656
Wondering how much is the salary of BCom graduates? Then you’ve come to the right place. Commerce is one of the most popular streams among India
Read More

by Sriram

10 Sep 2023

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon