Many of us read about Armstrong Number specially when we are learning programming and very curious to know new things and explore new terms and develop new programs.
So, to answer all those questions related to Armstrong Number.......
What is Armstrong Number ?
It is a number that is the sum of its own digits each raised to the power of the number of digits
e.g, 153 = 1^3 + 5^3 + 3^3
the above example shows that the number (153) is an Armstrong Number.
How to find it using Java Program ?
Its very simple to do so in java , Have a look at the given java program :
Output :
So, to answer all those questions related to Armstrong Number.......
What is Armstrong Number ?
It is a number that is the sum of its own digits each raised to the power of the number of digits
e.g, 153 = 1^3 + 5^3 + 3^3
the above example shows that the number (153) is an Armstrong Number.
How to find it using Java Program ?
Its very simple to do so in java , Have a look at the given java program :
import java.util.Scanner; public class armstrong { public static int armstrong(int num){ int armNum=0 , len=(""+num).length(); for(int i=len-1;i>=0;i--) { int temp=(int) (num/(Math.pow(10,i))); armNum=(int) (armNum + Math.pow(temp, len)); num=(int) (num%Math.pow(10, i)); } return armNum; } public static void main(String arg[]){ Scanner scan = new Scanner(System.in); System.out.print("Enter Number: "); int num=scan.nextInt(); if(armstrong(num)==num){ System.out.println(num+" is an Armstrong Number"); } else{ System.out.println(num+" is not an Armstrong Number"); } } }
Output :
1 Comment. Leave new
Another simple way to check if a number is an Armstrong number by taking user input...
Replypackage com.practice.java;
import java.util.Scanner;
class Armstrong{
public static void main(String args[]){
System.out.println("Enter a number");
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
int size = a.length();
System.out.println("Length of the input number is = " + size);
int num = Integer.parseInt(a);
int n = num;
int check=0,remainder;
while(num > 0){
remainder = num % 10;
check = check + (int)Math.pow(remainder,size);
num = num / 10;
}
if(check == n)
System.out.println(n+" is an Armstrong Number");
else
System.out.println(n+" is not an Armstrong Number");
sc.close();
}
}
Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.