2017-10-21 21:10:01 +08:00
|
|
|
import java.util.Scanner;
|
2017-10-22 01:52:07 +08:00
|
|
|
|
2017-10-21 21:10:01 +08:00
|
|
|
/**
|
2017-10-22 01:52:07 +08:00
|
|
|
* A utility to check if a given number is armstrong or not. Armstrong number is
|
|
|
|
* a number that is equal to the sum of cubes of its digits for example 0, 1,
|
|
|
|
* 153, 370, 371, 407 etc. For example 153 = 1^3 + 5^3 +3^3
|
|
|
|
*
|
2017-10-21 21:10:01 +08:00
|
|
|
* @author mani manasa mylavarapu
|
2017-10-22 01:52:07 +08:00
|
|
|
*
|
2017-10-21 21:10:01 +08:00
|
|
|
*/
|
|
|
|
public class Armstrong {
|
2018-12-19 22:02:39 +08:00
|
|
|
static Scanner scan;
|
2017-10-21 21:10:01 +08:00
|
|
|
public static void main(String[] args) {
|
2018-12-19 22:02:39 +08:00
|
|
|
scan = new Scanner(System.in);
|
|
|
|
int n = inputInt("please enter the number");
|
2017-10-21 21:10:01 +08:00
|
|
|
boolean isArmstrong = checkIfANumberIsAmstrongOrNot(n);
|
2017-10-22 01:52:07 +08:00
|
|
|
if (isArmstrong) {
|
2017-10-21 21:10:01 +08:00
|
|
|
System.out.println("the number is armstrong");
|
2017-10-22 01:52:07 +08:00
|
|
|
} else {
|
2017-10-21 21:10:01 +08:00
|
|
|
System.out.println("the number is not armstrong");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 01:52:07 +08:00
|
|
|
/**
|
|
|
|
* Checks whether a given number is an armstrong number or not. Armstrong
|
|
|
|
* number is a number that is equal to the sum of cubes of its digits for
|
|
|
|
* example 0, 1, 153, 370, 371, 407 etc.
|
|
|
|
*
|
|
|
|
* @param number
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-10-21 21:10:01 +08:00
|
|
|
public static boolean checkIfANumberIsAmstrongOrNot(int number) {
|
2017-10-22 01:52:07 +08:00
|
|
|
int remainder, sum = 0, temp = 0;
|
|
|
|
temp = number;
|
2017-10-21 21:10:01 +08:00
|
|
|
while (number > 0) {
|
|
|
|
remainder = number % 10;
|
|
|
|
sum = sum + (remainder * remainder * remainder);
|
|
|
|
number = number / 10;
|
|
|
|
}
|
|
|
|
if (sum == temp) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-19 22:02:39 +08:00
|
|
|
}
|
|
|
|
private static int inputInt(String string) {
|
|
|
|
System.out.print(string);
|
|
|
|
return Integer.parseInt(scan.nextLine());
|
2017-10-21 21:10:01 +08:00
|
|
|
}
|
|
|
|
}
|