An Armstrong number is a 3 digit number for which sum of cube of its digit is equal to the number. Example of Armstrong number is 153 as 153= 1+ 125+27 which 1^3+5^3+3^3. Another Armstrong number is 371.
Example:
import java.util.Scanner;
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();
int result = 0;
int orig = number;
while(number != 0){
int remainder = number%10;
result = result + remainder*remainder*remainder;
number = number/10;
}
if(orig == result){
System.out.println("Number : " + orig + " is an Armstrong number");
}else{
System.out.println("Number : " + orig + " is not an Armstrong number");
}
}
}
No comments:
Post a Comment