# Java Program to Check Armstrong Number

## What is  an Armstrong ?

An Armstrong number, also known as a narcissistic number, is a number that is the sum of its own digits when each digit is raised to the power of the number of digits in the number. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371.

Understanding Armstrong numbers can be a fun way to explore the world of numbers and their intriguing properties.

## Characteristics of Armstrong Numbers

Armstrong numbers have some interesting characteristics:

• They are positive integers.
• The number of digits in an Armstrong number is finite.
• The sum of each digit raised to the power of the total number of digits results in the original number itself.
• Armstrong numbers are relatively rare compared to other numbers.

## Algorithm for Armstrong Number in Java

1. Define a function isArmstrongNumber() that takes an integer num as input and returns a boolean value.
2. Declare a variable originalNum and set it equal to num.
3. Convert num to a string and find the length of the string. This will give us the number of digits in num.
4. Declare a variable sum and set it to 0. This will be used to store the sum of the digits raised to the power of the number of digits.
5. Begin a loop that will iterate as long as num is greater than 0.
6. Inside the loop, find the last digit of num by using the modulus operator (%) to get the remainder when num is divided by 10.
7. Raise the digit to the power of the number of digits, and add the result to sum.
8. Divide num by 10 to remove the last digit.
9. End the loop.
10. After the loop, check if originalNum is equal to sum. If it is, return true. Otherwise, return false.

### Here is a pseudo-code algorithm for checking whether a given number is an Armstrong number:

FUNCTION isArmstrongNumber(num: INTEGER) -> BOOLEAN:
originalNum = num
numOfDigits = LENGTH(num AS STRING)
sum = 0
WHILE num > 0:
digit = num MOD 10
sum = sum + digit^numOfDigits
num = num DIV 10
END WHILE
RETURN originalNum == sum
END FUNCTION

#### Here are a few examples of Armstrong numbers:

• 153 (1^3 + 5^3 + 3^3 = 153)
• 371 (3^3 + 7^3 + 1^3 = 371)
• 9474 (9^4 + 4^4 + 7^4 + 4^4 = 9474)

### Example :

Run

public class Main {
public static void main(String[] args) {
int num = 153;
if (isArmstrongNumber(num)) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is not an Armstrong number.");
}
}

public static boolean isArmstrongNumber(int num) {
int originalNum = num;
int numOfDigits = String.valueOf(num).length();
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum = sum + (int) Math.pow(digit, numOfDigits);
num = num / 10;
}
return originalNum == sum;
}
}

### Output :

153 is an Armstrong number.

### Example :

Run

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter a number (or q to quit): ");
String input = scanner.nextLine();
if (input.equals("q")) {
break;
}
int num = Integer.parseInt(input);
if (isArmstrongNumber(num)) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is not an Armstrong number.");
}
}
}

public static boolean isArmstrongNumber(int num) {
int originalNum = num;
int numOfDigits = String.valueOf(num).length();
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum = sum + (int) Math.pow(digit, numOfDigits);
num = num / 10;
}
return originalNum == sum;
}
}

### Input :

Enter a number (or q to quit): 153

### Output :

153 is an Armstrong number.

Note : This modified program will prompt the user to enter a number, and will keep prompting until the user enters “q” to quit. For each number entered, the program will print a message indicating whether the number is an Armstrong number or not.

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others