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
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
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
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
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 javaBtw, 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