Java Program to Check Armstrong Number - The Coding Shala
Home >> Java Programs >> Armstrong Number
Other Posts You May Like
In this post, we will learn how to check if a given number is Armstrong Number in Java.
Java Program to Check Armstrong Number
A positive number is called Armstrong Number if the following equation is true:
ab...z = a^n + b^n + .... + z^n
Here, n is the number of digits in the number.
For example, let's take 3 digits Armstrong Number.
Example 1:
Input: 153
Output: Armstrong Number
Explanation:
153 = 1*1*1 + 5*5*5 + 3*3*3
= 1 + 125 + 27
= 153
Java Program:
// Java program to Check Armstrong Number public class Main { public static void main(String[] args) { // input number to check Armstrong int number = 370; int sum = 0, temp = number; while(temp != 0) { int digit = temp % 10; temp = temp / 10; sum += (digit * digit * digit); } if(sum == number) { System.out.println(number + " is a Armstrong number"); } else { System.out.println(number + " is not a Armstrong number"); } } }
Output:
370 is a Armstrong number
- Java Program to Reverse a String using Recursion
- Java Program to Find the Sum of N numbers
- Java Program to Swap Two Numbers
- Java Program to Multiply Two Numbers
- Java Program to Find the Sum of Digits of a Number
Comments
Post a Comment