forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Armstrong.java
49 lines (44 loc) · 1.46 KB
/
Armstrong.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package Others;
import java.util.Scanner;
/**
* 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
*
* @author mani manasa mylavarapu
*/
public class Armstrong {
static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
int n = inputInt("please enter the number");
boolean isArmstrong = checkIfANumberIsAmstrongOrNot(n);
if (isArmstrong) {
System.out.println("the number is armstrong");
} else {
System.out.println("the number is not armstrong");
}
}
/**
* 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
*/
public static boolean checkIfANumberIsAmstrongOrNot(int number) {
int remainder, sum = 0, temp = 0;
temp = number;
while (number > 0) {
remainder = number % 10;
sum = sum + (remainder * remainder * remainder);
number = number / 10;
}
return sum == temp;
}
private static int inputInt(String string) {
System.out.print(string);
return Integer.parseInt(scan.nextLine());
}
}