Armstrong Number in Java
Check whether or Not the Number is an Armstrong Number in Java
Given an integer input the objective is to check whether or not the number is an Armstrong number. To do so we check if the number satisfies the below mentioned conditions and if it does, the it’s an Armstrong. The task is to write a code to Check Whether or Not the Number is an Armstrong Number in Java Language.
Example
Input : 371
Output : It's an Armstrong Number.
Check Whether or Not the Number is an Armstrong Number
Given an integer input, the objective is to check whether or not the number input is an Armstrong number or not.
However, Armstrong number is any number following the given rule –
- abcd… = an + bn + cn + dn + …
- Where n is the order(length/digits in number)
Also check – Armstrong Number in a given Range in Java
Input : 371
Output : It's an Armstrong Number.
Explanation: 371 = 3^3 + 7^3 + 1^3
Therefore it's an Armstrong number.
Method 1: Using Iteration
In this method we’ll use loops to check for Whether or not the number input is an Armstrong Number.
Working
For an integer input number, we do the following operations
- Run a for loop to extract the last digit and break the number by size-1 with every iteration.
- With each iteration raise the extracted digit to the power of the length of the number.
- Append the value to the sum variable.
- Print the sum variable when the loop is terminated.
Let’s implement the above logic in Java Language.
Java Code
public class Main { public static void main (String[]args) { int num = 407, len; // function to get order(length) len = order (num); // check if Armstrong if (armstrong (num, len)) System.out.println(num + " is armstrong"); else System.out.println(num + " is armstrong"); } static int order (int x) { int len = 0; while (x != 0 ) { len++; x = x / 10; } return len; } static boolean armstrong (int num, int len) { int sum = 0, temp, digit; temp = num; // loop to extract digit, find power & add to sum while (temp != 0) { // extract digit digit = temp % 10; // add power to sum sum = sum + (int)Math.pow(digit, len); temp /= 10; }; return num == sum; } }
Output
407 is armstrong
Method 2: Using Recursion
In this method we’ll use Recursion to check whether or not the number is an Armstrong Number. To know more about recursion in Java check out, Java Recursion .
Working
For an input integer, we do the following
- Define a recursive function with base case as number == 0.
- Set recursive step call as checkArmstrong(num/10, length).
- Keep append the sum variable with each recursive call and return the sum variable.
- Print the sum variable.
Let’s implement the above logic in Java Language.
Java Code
import java.util.*; import java.lang.*; class Main { // Driver code public static void main(String[] args) { //variables initialization int num = 1634, reverse = 0; int len = order(num); if (num == getArmstrongSum(num, len)) System.out.println(num + " is an Armstrong Number"); else System.out.println(num + " is not an Armstrong Number"); } private static int getArmstrongSum(int num, int order) { if(num == 0) return 0; int digit = num % 10; return (int) Math.pow(digit, order) + getArmstrongSum(num/10, order); } private static int order(int num) { int len = 0; while (num!=0) { len++; num = num/10; } return len; } }
Output
1634 is an Armstrong Number
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
30+ Companies are Hiring
Get Hiring Updates right in your inbox from PrepInsta
When I clicked “Find the Nth Term of the Fibonacci Series” in JAVA: It is showing the results of “Fibonacci Series upto nth term” & When I’m clicked “Factorial of a number” in JAVA: It is showing the results of “Find the Nth Term of the Fibonacci Series”. Please Solve this problem.
Hey there, Kindly join our Discord server for all the technical and subject related doubts, our mentors will guide you further.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int a=in.nextInt();
System.out.println(amstrong_no(a));
}
static boolean amstrong_no(int a){
int original=a;
int sum=0;
while (a>0){
int remainder=a%10;
sum=sum+remainder*remainder*remainder;
a=a/10;
}
if(original==sum){
return true;
}
return false;
}
}
public class Column {
public static void main(String[] args){
int num = 370;
int temp = num;
int sum=0;
String power = Integer.toString(num);
if(num == 0 ){
System.out.println(“Armstrong Number”);
}
while(temp!=0){
int digit = temp%10;
sum = sum+(int)(Math.pow(digit,power.length()));
temp = temp/10;
}
if (num == sum){
System.out.println(“Its a Armstrong Number”);
}else {
System.out.println(“Its not a Armstrong Number”);
}
}
}
public class Armstrong_number {
public static void main(String[] args) {
String num = “153”;
int num1 = Integer.parseInt(num);
int reverse = 0, reminder, temp;
temp = num1;
while (temp != 0) {
reminder = temp % 10;
reverse = (int) (reverse + Math.pow(reminder, num.length()));
temp /= 10;
}
if (reverse == num1) {
System.out.println(“The given number” + num + ” is Armstrong number”);
} else {
System.out.println(“The given number ” + num + ” is NOT a Armstrong number”);
}
}
}
check it out!!
public class Interview2 {
public static void main(String[] args){
//Armstrong Number;
int a = 1234;
String b = a+””;
int sum = 0;
int num;
while(a!=0){
num = a%10;
sum = (int) (sum+Math.pow(num,b.length()));
a = a/10;
}
System.out.println(sum);
}
}
import java.util.*;
import java.lang.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the Number”);
int num = sc.nextInt();
int len = order(num);
if(num == armstrong(num,len)){
System.out.println(num + “is armstrong”);
}
else{
System.out.println(num + ” is not armstrong”);
}
}
private static int order(int x){
int len = 0;
while(x != 0){
len++;
x = x/10;
}
return len;
}
private static int armstrong(int num, int len){
if(num == 0){
return 0;
}
int digit = num %10;
return (int)Math.pow(digit,len) + armstrong(num/10,len);
}
}
public class test {
public static void main(String[] args) {
int num = 567, number, temp, total = 0;
number = num;
while (number != 0)
{
temp = number % 10;
total = total + temp*temp*temp;
number /= 10;
}
if(total == num)
System.out.println(num + ” is an Armstrong number”);
else
System.out.println(num + ” is not an Armstrong number”);
}
}