-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode10.cpp
More file actions
59 lines (44 loc) · 1.13 KB
/
code10.cpp
File metadata and controls
59 lines (44 loc) · 1.13 KB
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
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int num1, num2, i, num, digit, sum, count;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
// swap if num1 > num2
if (num1 > num2) {
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
}
cout << "Armstrong numbers between " << num1 << " and " << num2 << " are: " << endl;
// print Armstrong numbers from num1 to num2
for(i = num1; i <= num2; i++) {
// count gives the number of digits in i
count = 0;
// store value of i in num
num = i;
// count the number of digits in num and i
while(num > 0) {
++count;
num /= 10;
}
// initialize sum to 0
sum = 0;
// store i in num again
num = i;
// get sum of power of all digits of i
while(num > 0) {
digit = num % 10;
sum = sum + pow(digit, count);
num /= 10;
}
// if sum is equal to i, then it is Armstrong
if(sum == i) {
cout << i << ", ";
}
}
return 0;
}