-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_Q#5a_armstrong.cpp
61 lines (44 loc) · 1.12 KB
/
func_Q#5a_armstrong.cpp
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
60
61
// ******Arm Strong Number******
#include <iostream>
#include <sstream>
using namespace std;
// function for armstrong
void ast_chk(int n)
{
int len, cnt,num;
// for the conversion of int to string
stringstream ss;
string str;
ss << n;
ss >> str;
// len of string for loop iteration use;
len = str.length();
// string to integer conversion
num = stoi(str);
int x = 1;
cnt = 0;
for (int i = 0; i < len; i++)
{
int di;
// seperating the digits form integer: https://stackoverflow.com/questions/4261589/how-do-i-split-an-int-into-its-digits
di = (num / x) % 10;
x *= 10;
// Cubing every digit and storing in cnt.
cnt += (di * di * di);
}
cout << "The sum of cubes of every digit is: " << cnt << endl;
// conditions for armstrong
if (cnt == num)
{
cout << "The number is ArmStrong!" << endl;
}
else if (cnt != num)
{
cout << "The number is Not ArmStrong!" << endl;
}
}
int main()
{
ast_chk(153);
return 0;
}