Armstrong Number in Java
Login/Signup to comment
8 comments on “Armstrong Number in Java”
×
30+ Companies are Hiring
Get Hiring Updates right in your inbox from PrepInsta
Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
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.
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 –
Also check – Armstrong Number in a given Range in Java
In this method we’ll use loops to check for Whether or not the number input is an Armstrong Number.
For an integer input number, we do the following operations
Let’s implement the above logic in Java Language.
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; } }
407 is armstrong
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 .
For an input integer, we do the following
Let’s implement the above logic in Java Language.
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; } }
1634 is an Armstrong Number
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
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”);
}
}