Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

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

Updated on 14 October, 2022

7.2K+ views
6 min read

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.

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. 

Frequently Asked Questions (FAQs)

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

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

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

RELATED PROGRAMS