Java Check Armstrong Number of N Digits Program

By | August 4, 2019

Task – Java Check Armstrong Number Program. This Java Programming Tutorial will explain the source code of Armstrong Number checking program. The Java program will ask you to enter an integer number (whole number). When you will enter a number, this Java Check Armstrong Number program will find whether the given number is an Armstrong number or not.

What is an Armstrong Number?

An armstrong Number of N digits is an integre such that the sum of its every digit raised to the power N is equal to the number itself.
For example, consider the integer number 153. Since, in this case N=3 which is the number of digits in 153.
Hence the sum of digits 1,5,3 raised to the power 3 is calculated as:
Num = 153
Sum = 13 + 53 + 33
Hence
Sum = 1 + 125 + 27
Sum = 153
Since, we find that Num is equal to the Sum of cubes of the digits, therefore, we conclude that 153 is an Armstrong Number. Because the sum of cubes of its digits is equal to the number itself.

An Armstrong Number of Three Digits

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For

example, 370 is an Armstrong number since 3**3 + 7**3 + 0**3 = 370. Note that ‘**’ represent power operation.

Java Armstrong Number Check Program

Java Armstrong Number Check Program

The Source Code of Java Check Armstrong Number Program

/*
 * Write a Java program to input 
 * a number and check whether it is 
 * an Armstrong Number?
 */
package checkarmstrongnumber;

/**
*
* @author www.EasyCodeBook.com
*/
import java.util.Scanner;
public class  CheckArmstrongNumber {

public static  void main(String[] args) {
   int num, n, digit, digits, power, sum=0;

Scanner input = new Scanner(System.in);

System.out.print(“Enter a number to check for Armstrong Number:”);
num = input.nextInt();

String s = String.valueOf(num);
digits = s.length();
power = digits;

n = num;

while (n != 0)
{
digit = n % 10;
sum = sum + (int)Math.pow(digit,power);
n = n / 10;
}

if(num == sum)
System.out.println(num+” is an Armstrong Number”);
else
System.out.println(num+” is not an Armstrong Number”);

}

}


Java Check Armstrong Number Program –

Output – Different Sample Runs

Enter a number to check for Armstrong Number:1634
1634 is an Armstrong Number

Enter a number to check for Armstrong Number:153
153 is an Armstrong Number

Enter a number to check for Armstrong Number:407
407 is an Armstrong Number

Enter a number to check for Armstrong Number:456
456 is not an Armstrong Number

Enter a number to check for Armstrong Number:1
1 is an Armstrong Number

Enter a number to check for Armstrong Number:371
371 is an Armstrong Number

Enter a number to check for Armstrong Number:370
370 is an Armstrong Number

Loading

One thought on “Java Check Armstrong Number of N Digits Program

  1. Vannessa Thongchanh

    I dugg some of you post as I thought they were very useful handy

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *