Skip to content

Commit f93e145

Browse files
Merge pull request #174 from vish-han/patch-4
Armstrong number added
2 parents dacbe27 + e697204 commit f93e145

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Number Theory/ArmstrongNumber.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers;
2+
/* 153 = (1*1*1)+(5*5*5)+(3*3*3)
3+
where:
4+
(1*1*1)=1
5+
(5*5*5)=125
6+
(3*3*3)=27
7+
So:
8+
1+125+27=153 */
9+
10+
public class ArmstrongNumber {
11+
public ArmstrongNumber() {
12+
}
13+
14+
public static void main(String[] args) {
15+
Scanner in = new Scanner(System.in);
16+
int num = in.nextInt();
17+
int temp = num;
18+
19+
int result;
20+
//we will break the number into individual digits and cube it and then
21+
//sum the cubes of individual digits
22+
for(result = 0; 0 < num; num /= 10) {
23+
int rem = num % 10;
24+
result += rem * rem * rem;
25+
}
26+
// we check if the number is armstrong or not
27+
if (temp == result) {
28+
System.out.println(temp + " is a armstrong number");
29+
} else {
30+
System.out.println(temp + " is not a armstrong number");
31+
}
32+
33+
}
34+
}

0 commit comments

Comments
 (0)