Java Program to find Armstrong numbers with Example

How to check if a number is Armstrong number or not? or write a Java program to find Armstrong number? This is a common Java interview question asked on campus interviews and fresher level interviews. This is also a popular Java programming exercise on various school, colleges and computer courses to build programming logic among Students. An Armstrong number is a 3 digit number for which sum of cube of its digits is equal to the number itself. An example of Armstrong number is 153 as 153= 1+ 125+27 which is equal to 1^3+5^3+3^3. One more example of the Armstrong number is 371 because it is the sum of 27 + 343 + 1 which is equal to 3^3 + 7^3 + 1^3 . In this Java program example, we will see complete code example of Java program to check if any 3 digit number is Armstrong number or not. If you are going for Java interview, then be prepare for some follow-up questions e.g. finding prime numbers, or finding Armstrong number of more than 3 digits.
Armstrong numbers

By the way, this Java program is in continuation of our earlier programming exercise like
start programming in java
If you haven't read them already you may find them useful as programming exercise or interview question. Alternatively, you can also check out Cracking the Coding Interview book, which contains more than 190 programming questions and solutions start programming in java

How to check if number is Armstrong number in Java

How to find Armstrong number in Java with Example
Here is complete code for checking if a number is Armstrong number or not. It uses a method called isArmstrong(int number) to implement logic for checking if a number is Armstrong nor not. start programming in java

Btw, this is not the same program as print all Armstrong number between 0 and 999 but you can use this logic to solve that question as well. All you need to do is loop till 1000 and check if the number is Armstrong or not. If yes then print otherwise move to next number.
 start programming in java

Java Program to Find Armstrong Number
import java.util.Scanner;

/**
* Simple Java Program to check or find if a number is Armstrong number or not.
* An Armstrong number of three digit is a number whose sum of cubes of its digit is equal
* to its number. For example 153 is an Armstrong number of 3 digit because 1^3+5^3+3^3 or
   1+125+27=153
* @author Sonia
*/
public class ArmstrongTest{


public static void main(String args[]) {

//input number to check if its Armstrong number
System.out.println("Please enter a 3 digit number to find if
its an Armstrong number:"
);
int number = new Scanner(System.in).nextInt();

//printing result
if(isArmStrong(number)){
System.out.println("Number : " + number + " is an Armstrong number");
}else{
System.out.println("Number : " + number + " is not an Armstrong number");
}


}

/*
* @return true if number is Armstrong number or return false
*/
private static boolean isArmStrong(int number) {
int result = 0;
int orig = number;
while(number != 0){
int remainder = number%10;
result = result + remainder*remainder*remainder;
number = number/10;
}
//number is Armstrong return true
if(orig == result){
return true;
}

return false;
}

}

Output:
Please enter a 3 digit number to find if its an Armstrong number:
153
Number : 153 is an Armstrong number
Please enter a 3 digit number to find if its an Armstrong number:
153
Number : 153 is an Armstrong number
Please enter a 3 digit number to find if its an Armstrong number:
371
Number : 371 is an Armstrong number


That's all on How to check if a number is Armstrong in Java. It’s pretty simple Java program and if you look closely it just gets digit by digit by using remainder operator and reduce number by 1 digit after dividing it by 10. Let me know if you find any bug on this Java program for checking Armstrong number. start programming in java


Other Java programming problems you may like : start programming in java
start programming in java
Java Program to find Armstrong numbers with Example Java Program to find Armstrong numbers with Example Reviewed by Sonia Rizvi on 06:05 Rating: 5

No comments:

Powered by Blogger.