Java Program - Check Armstrong Number
A positive natural number is known as Armstrong number of order n if it can be expressed as the sum of each digits of the number raised to the power of n. Mathematically, it can be expressed as:
Examples:
153 = 13+53+33 = 1+125+27 = 153
371 = 33+73+13 = 27+343+1 = 371
1634 = 14+64+34+44 = 1+1296+81+256 = 1634
Method 1: Check Armstrong Number
In the example below, the MyNum is checked for Armstrong number using method called ArmStrongNum(). The method requires two parameters, first the number and second the number of digits in it. It calculates power of the digit using the Pow() method. See the example below for syntax:
public class MyClass { //Calculate power of a digit static int Pow(int MyNum, int n) { int x = 1; while(n > 0) { x = x*MyNum; n--; } return x; } static void ArmStrongNum(int MyNum, int Order) { int y = MyNum; int sum = 0; while (y > 0){ int x = y % 10; sum = sum + Pow(x, Order); y = y/10; } if (MyNum == sum){ System.out.println(MyNum + " is a Armstrong Number."); } else { System.out.println(MyNum + " is not a Armstrong Number."); } } public static void main(String[] args) { ArmStrongNum(371, 3); ArmStrongNum(1634, 4); ArmStrongNum(1000, 4); } }
The above code will give the following output:
371 is a Armstrong Number. 1634 is a Armstrong Number. 1000 is not a Armstrong Number.
Method 2: Armstrong Number of order n
In this example, ArmStrongNum method requires only one parameter, the number itself. The number of digits in the passed parameter is estimated inside the method.
public class MyClass { //Calculate power of a digit static int Pow(int MyNum, int n) { int x = 1; while(n > 0) { x = x*MyNum; n--; } return x; } static void ArmStrongNum(int MyNum) { int y = MyNum; int sum = 0; int Order = 0; //Find number of digit in the Number while(y > 0){ Order++; y = y / 10; } y = MyNum; while (y > 0){ int x = y % 10; sum = sum + Pow(x, Order); y = y/10; } if (MyNum == sum){ System.out.println(MyNum + " is a Armstrong Number."); } else { System.out.println(MyNum + " is not a Armstrong Number."); } } public static void main(String[] args) { ArmStrongNum(153); ArmStrongNum(9474); ArmStrongNum(5000); } }
The above code will give the following output:
153 is a Armstrong Number. 9474 is a Armstrong Number. 5000 is not a Armstrong Number.
Recommended Pages
- Java Program - To Check Prime Number
- Java Program - Bubble Sort
- Java Program - Selection Sort
- Java Program - Maximum Subarray Sum
- Java Program - Reverse digits of a given Integer
- Java - Swap two numbers
- Java Program - Fibonacci Sequence
- Java Program - Insertion Sort
- Java Program - Find Factorial of a Number
- Java Program - Find HCF of Two Numbers
- Java Program - Merge Sort
- Java Program - Shell Sort
- Stack in Java
- Queue in Java
- Java Program - Find LCM of Two Numbers
- Java Program - To Check Whether a Number is Palindrome or Not
- Java Program - To Check Whether a String is Palindrome or Not
- Java Program - Heap Sort
- Java Program - Quick Sort
- Java - Swap Two Numbers without using Temporary Variable