• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Armstrong Number in Java with Different Methods
Next →
← Prev

Armstrong Number in Java with Different Methods

By Dinesh Thakur

In this Armstrong Number in Java tutorial, we will write a java program for armstrong number (also known as narcissistic number). Before we proceed through the program, let us see what’s an Armstrong number, after which we’ll check whether a specified number is Armstrong number or not, and see the number of variants of the same program.

We’ll be covering the following topics in this tutorial:

  • What is an Armstrong Number?
  • Java Program for Armstrong number

What is an Armstrong Number?

In an Armstrong Number, the sum of individual digits’ power is equivalent to the number itself. Between 1 to 1000, you will find five Armstrong numbers. They Are :- 1, 153, 370, 371, 407.
Armstrong number is a unique sort of number, where every digit is raised to the power of the number of digits and ultimately summed of digits to get a number. If the number thus discovered is the same as the number initially taken, then the respective number is called an Armstrong number. A good illustration of Armstrong’s number is 370. when we break down the digits of 370, they’re 3, 7, and 0. Then we locate every digit raised to the power of the number of digits, and at last, we calculate the sum of the numbers.

Here’s the general equation.

abcd = an + bn + cn + dn + …

where n denotes the number of digits in the number

For example this is a Three digit Armstrong number

Armstrong number in java


Example 1: 370
370 = (3)3 + (7)3 + (0)3
= 27 + 343 + 0
= 370
Four Digits Armstrong number
Example 2: 1634
1634 = (1)4 + (6)4 + (3)4 + (4)4
= 1 + 1296 + 81 + 256
= 1634

Java Program for Armstrong number

You can check whether a given number is Armstrong number or not in Java in number of ways:

Using ‘while’ loop
Using ‘for’ loop
Using Recursion
Using BufferedReader

Program to check Armstrong Number for a Three digit number (Using while Loop)

Here’s the very first sample program utilizing a while loop. While loop is only implementing a set of statements repeatedly as long as the condition is true. In cases like this, we’ve taken the numbers from 0 to N. Upon execution, you’ll be able to understand what Armstrong’s number is. Check out the output below so one can find an idea.

// Armstrong Number in Java Programming
public class Armstrong {
 public static void main(String[] args) {
 int number = 371, InitialNumber, remainder, result = 0;
 InitialNumber = number;
 while (InitialNumber != 0) {
  remainder = InitialNumber % 10;
  result += Math.pow(remainder, 3);
  InitialNumber /= 10;
 }
  if(result == number)
   System.out.println(number + " is an Armstrong number."); 
  else
   System.out.println(number + " is not an Armstrong number.");
 }
}

Output:

armstrong number program in java

• First, given number value store in a different integer variable, InitialNumber. We will need to compare the values of the final number and the Initial number at the end.
• Subsequently, a while loop used to loop through InitialNumber till it’s equivalent to 0.
• On every iteration, the final digit of the num store in the remainder.
• Afterward, the remainder is powered by 3 (number of digits) with Math.pow() function and added to the outcome.
• Subsequently, the final digit eliminate from InitialNumber after division by 10.
• Finally, results and numbers compare. If equivalent, it’s an Armstrong number. Otherwise, it is not.

Java example to check whether the input number is Armstrong Number or not (Using for Loop)

• We Utilize ‘for loop’ to Get iterate from 0 until N.
• For any number X (0< X < N), find the cubic sum of X digits and store it in the sum variable.
• Evaluate X and sum.
• If both are equivalent, then X is an Armstrong number otherwise not an Armstrong number.

// Armstrong Number in Java Programming
import java.util.Scanner;
public class Armstrong {
 public static void main(String[] args) {
  int num, number, temp, total = 0;
  System.out.println("Enter 3 Digit Number");
  Scanner scanner = new Scanner(System.in);
  num = scanner.nextInt();
  scanner.close();
  number = num;
  for( ;number!=0;number /= 10) {
   temp = number % 10;
   total = total + temp*temp*temp;
  }
 if(total == num)
  System.out.println(num + " is an Armstrong number");
 else
  System.out.println(num + " is not an Armstrong number");
 }
}

Output:

java program for armstrong number

Java Program for Armstrong Number (Using Recursion)

Here is complete code for checking if a number is Armstrong number or not. It uses a method called isArmstrong(int numX, int numY) to implement the logic for checking if a number is Armstrong nor not.

// Armstrong Number in Java Programming
class ArmstrongwithRecursion {
  int i;
  int isArmstrong(int numX, int numY) {
   if(numX!=0) {
     x=numX%10;
     numY = numY +(i*i*i);
     numX/=10 ;
     return isArmstrong(numX,numY);
   }
   return numY;
 }
 public static void main(String[] args) {
   ArmstrongwithRecursion AR = new ArmstrongRecursion();
   int arm;
   System.out.println("Armstrong numbers between 1 to 1000");
   for(int num=1;num&amp;lt;500;num++) {
     arm=AR.isArmstrong(num,0);
     if(arm==num)
      System.out.println(num);
   }
 }
}

Java program to check the input number is Armstrong Number or not (Using BufferedReader)

// Armstrong Number in Java Programming
class ArmstrongBufferedReader {
  public static void main(String[] arg) throws IOException {
   int result,Strong = 0,num,temp;
   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Enter a Number");
   num = Integer.parseInt(in.readLine());
   temp=num;
   while(num!=0) {
    result = num%10;
    Strong = Strong+(result*result*result);
    num = num/10;
  }
  if(Strong == temp)
   System.out.println(temp+" is a Armstrong Number ");
  else
   System.out.println(temp+" is not a Armstrong Number ");
 }
}

You’ll also like:

  1. Armstrong Number in Java Example
  2. Armstrong Number With in Range Java Example
  3. Class methods in Java
  4. Constructor Methods in java
  5. Finalizer methods in Java
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW