- Write a java program to check whether a number is armstrong number or not.

In this java program, we have to check whether a given number is an armstrong number or not and print the result on screen.

**Armstrong number** is a number that is equal to the sum of cubes of its digits.

Examples of Armstrong Numbers : 0, 1, 2, 3, 153, 370, 407 etc.

153 is an Armstrong number

153 = 1*1*1 + 5*5*5 + 3*3*3

100 is not an Armstrong number

100 is not equal to 1*1*1 + 0*0*0 + 0*0*0

**Algorithm to check for Armstrong number**

Let N be the given.

- Find the cubic sum of digits of N, and store it in sum variable.
- Compare N and sum.
- If both are equal then N is Armstrong number otherwise not an Armstrong number.

## Java program to check Armstrong number or not using loop

This program first takes a number as input from user and stores it in variable 'N'. It makes a copy of number in variable 'temp'. Then using a while loop, it calculates the sum of cubes of every digit of temp(loop will terminate when temp becomes zero) and stores it in a 'sum' variable. If sum is equal to N then it is an Armstrong number otherwise N is not an Armstrong number.

package com.tcc.java.programs; import java.util.Scanner; /** * Java Program to check Armstrong Number */ public class ArmstrongNumber { public static void main(String[] args) { int N, temp, sum = 0, rightDigit; Scanner scanner; scanner = new Scanner(System.in); System.out.println("Enter an Integer"); N = scanner.nextInt(); temp = N; /* * Find the sum of cubes of every digit of N */ while (temp != 0) { rightDigit = temp % 10; sum = sum + (rightDigit * rightDigit * rightDigit); temp = temp / 10; } /* * Check if sum is equal to N, then N is a armstrong number otherwise * not an armstrong number */ if (sum == N) { // N is armstrong number System.out.format("%d is Armstrong Number", N); } else { // N is not an armstrong number System.out.format("%d is Not an Armstrong Number", N); } } }Output

Enter an Integer 407 407 is Armstrong Number

Enter an Integer 120 120 is Not an Armstrong Number

## Java program to check Armstrong number using function

This java program is similar to above program except here we are using a user defined function "isArmstrongNumber" which takes an integer input and prints whether input number is armstrong number or not.

package com.tcc.java.programs; import java.util.Scanner; /** * Java Program to check Armstrong Number */ public class ArmstrongNumberCheckFunction { public static void main(String[] args) { int N; Scanner scanner; scanner = new Scanner(System.in); System.out.println("Enter an Integer"); N = scanner.nextInt(); /* * Calling isArmstrongNumber method to check if N is armstrong or not. */ isArmstrongNumber(N); } public static void isArmstrongNumber(int N) { int sum = 0, rightDigit, temp; temp = N; while (temp != 0) { rightDigit = temp % 10; sum = sum + (rightDigit * rightDigit * rightDigit); temp = temp / 10; } /* * Check if sum is equal to N, then N is a armstrong number otherwise * not an armstrong number */ if (sum == N) { // N is armstrong number System.out.format("%d is Armstrong Number", N); } else { // N is not an armstrong number System.out.format("%d is Not an Armstrong Number", N); } } }Output

Enter an Integer 407 407 is Armstrong Number

**Recommended Posts**