**Armstrong number in java program** is a program which check whether given number is Armstrong number.

**Armstrong** **number** **in** **java**

**What is Armstrong number ? :- **Armstrong of a number is a sum of Nth power of each digits in number is equal to the number itself. Some Armstrong numbers are 0, 1, 153, 371 etc.

**Formula to find Armstrong number**

**Armstrong number Formula is as follows**

Number = Sum of Nth power of digits in number

Now let’s see examples to find Armstrong number

**Armstrong number Example**

Number = 153

Then sum = 1 ^ 3 + 5 ^ 3 + 3 ^ 3

Sum = 1 + 125 + 27

Sum = 153

So the number is Armstrong number.

**Logic for Armstrong number **

To find Armstrong number first we take a number. After that we calculate the sum of Nth power each digits. Now we compare sum value and the given number if both are equal then number is Armstrong number in java.

**Algorithm for Armstrong number**

- Start
- Declare variables
- Take a number user input
- Next iterate number from end to start using loop
- In loop separate each digital in number, add power of each digits
- Compare number and sum of both are equal then print number is Armstrong
- Print number is Armstrong
- End

## Armstrong number program in java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.*; public class Main { public static void main(String[] args) { int num,i,j,temp,sum=0;; Scanner sc=new Scanner(System.in); System.out.println("Enter a number to know whether it is armstrong or not"); num=sc.nextInt(); temp=num; while(num>0) { i=num%10; sum+=i*i*i; num=num/10; } if(sum==temp) System.out.println("Giveb number "+temp+" is an armstrong number"); else System.out.println("Giveb number "+temp+" is not an armstrong number since the sum of cubes of individual digits is "+sum); } } |

**Output :**

**Explanation :**

**1. First we declare required header files.**

**2. Start with writing main function.**

**3. Declare variables.**

Num => to store input number

i => to store separated digits.

sum => to store sum

temp => temporary variable

**4. Take input number**

=> num=sc.nextInt();

**5. Next copy number to temporary variable.**

temp = num;

So that we can further use temporary variable to check equality to sum.

**6. Next line while(num > 0) is to traverse the number from end till we encounter 0.**

Suppose for example num = 153

**At iteration 1:**

num = 153 > 0 this condition becomes true and while loop get executed

i=num%10 => i = 153 % 10 = 3

sum+=i*i*i => sum = 0 + 3 * 3 * 3 = 27

num=num/10 => num = 153 / 10 = 15

**At iteration 2:**

num = 15 > 0 this condition becomes true and while loop get executed

i=num%10 => i = 15 % 10 = 5

sum+=i*i*i => sum = 27 + 5 * 5 * 5 = 152

num=num/10 => num = 15 / 10 = 1

**At iteration 3:**

num = 1 > 0 this condition becomes true and while loop get executed

i=num%10 => i = 1 % 10 = 1

sum+=i*i*i => sum = 152 + 1 * 1 * 1 = 153

num=num/10 => num = 1 / 10 = 0

**At iteration 4:**

num = 0 > 0 this condition becomes false and while loop stops it’s execution.

**7. Now we have sum = 153 and temp = 153**

**8. Next we compare temp and sum**

if(sum==temp) If this condition is true then print number is Armstrong number.

In our example 153 ==153 => so condition becomes true and if block get executed in block print number is Armstrong number.

**9**. **Else print number is not Armstrong number as it’s sum of Nth power of each digits is not equal.**

**10. End with the Armstrong number program in java.**

## Leave a Reply