In this Armstrong Number in Java tutorial, we will write a java program for armstrong number (also known as narcissistic number). Before we proceed through the program, let us see what’s an Armstrong number, after which we’ll check whether a specified number is Armstrong number or not, and see the number of variants of the same program.
We’ll be covering the following topics in this tutorial:
What is an Armstrong Number?
In an Armstrong Number, the sum of individual digits’ power is equivalent to the number itself. Between 1 to 1000, you will find five Armstrong numbers. They Are :- 1, 153, 370, 371, 407.
Armstrong number is a unique sort of number, where every digit is raised to the power of the number of digits and ultimately summed of digits to get a number. If the number thus discovered is the same as the number initially taken, then the respective number is called an Armstrong number. A good illustration of Armstrong’s number is 370. when we break down the digits of 370, they’re 3, 7, and 0. Then we locate every digit raised to the power of the number of digits, and at last, we calculate the sum of the numbers.
Here’s the general equation.
abcd = an + bn + cn + dn + …
where n denotes the number of digits in the number
For example this is a Three digit Armstrong number
Example 1: 370 370 = (3)3 + (7)3 + (0)3 = 27 + 343 + 0 = 370 Four Digits Armstrong number Example 2: 1634 1634 = (1)4 + (6)4 + (3)4 + (4)4 = 1 + 1296 + 81 + 256 = 1634
Java Program for Armstrong number
You can check whether a given number is Armstrong number or not in Java in number of ways:
Using ‘while’ loop
Using ‘for’ loop
Using Recursion
Using BufferedReader
Program to check Armstrong Number for a Three digit number (Using while Loop)
Here’s the very first sample program utilizing a while loop. While loop is only implementing a set of statements repeatedly as long as the condition is true. In cases like this, we’ve taken the numbers from 0 to N. Upon execution, you’ll be able to understand what Armstrong’s number is. Check out the output below so one can find an idea.
// Armstrong Number in Java Programming public class Armstrong { public static void main(String[] args) { int number = 371, InitialNumber, remainder, result = 0; InitialNumber = number; while (InitialNumber != 0) { remainder = InitialNumber % 10; result += Math.pow(remainder, 3); InitialNumber /= 10; } if(result == number) System.out.println(number + " is an Armstrong number."); else System.out.println(number + " is not an Armstrong number."); } }
Output:
• First, given number value store in a different integer variable, InitialNumber. We will need to compare the values of the final number and the Initial number at the end.
• Subsequently, a while loop used to loop through InitialNumber till it’s equivalent to 0.
• On every iteration, the final digit of the num store in the remainder.
• Afterward, the remainder is powered by 3 (number of digits) with Math.pow() function and added to the outcome.
• Subsequently, the final digit eliminate from InitialNumber after division by 10.
• Finally, results and numbers compare. If equivalent, it’s an Armstrong number. Otherwise, it is not.
Java example to check whether the input number is Armstrong Number or not (Using for Loop)
• We Utilize ‘for loop’ to Get iterate from 0 until N.
• For any number X (0< X < N), find the cubic sum of X digits and store it in the sum variable.
• Evaluate X and sum.
• If both are equivalent, then X is an Armstrong number otherwise not an Armstrong number.
// Armstrong Number in Java Programming import java.util.Scanner; public class Armstrong { public static void main(String[] args) { int num, number, temp, total = 0; System.out.println("Enter 3 Digit Number"); Scanner scanner = new Scanner(System.in); num = scanner.nextInt(); scanner.close(); number = num; for( ;number!=0;number /= 10) { temp = number % 10; total = total + temp*temp*temp; } if(total == num) System.out.println(num + " is an Armstrong number"); else System.out.println(num + " is not an Armstrong number"); } }
Output:
Java Program for Armstrong Number (Using Recursion)
Here is complete code for checking if a number is Armstrong number or not. It uses a method called isArmstrong(int numX, int numY) to implement the logic for checking if a number is Armstrong nor not.
// Armstrong Number in Java Programming class ArmstrongwithRecursion { int i; int isArmstrong(int numX, int numY) { if(numX!=0) { x=numX%10; numY = numY +(i*i*i); numX/=10 ; return isArmstrong(numX,numY); } return numY; } public static void main(String[] args) { ArmstrongwithRecursion AR = new ArmstrongRecursion(); int arm; System.out.println("Armstrong numbers between 1 to 1000"); for(int num=1;num&lt;500;num++) { arm=AR.isArmstrong(num,0); if(arm==num) System.out.println(num); } } }
Java program to check the input number is Armstrong Number or not (Using BufferedReader)
// Armstrong Number in Java Programming class ArmstrongBufferedReader { public static void main(String[] arg) throws IOException { int result,Strong = 0,num,temp; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a Number"); num = Integer.parseInt(in.readLine()); temp=num; while(num!=0) { result = num%10; Strong = Strong+(result*result*result); num = num/10; } if(Strong == temp) System.out.println(temp+" is a Armstrong Number "); else System.out.println(temp+" is not a Armstrong Number "); } }