My goal is to create a code that will go through number list and search for armstrong numbers. If code finds one then he writes it out, if not, then he continues the loop until first number is larger than second one. I hope you got my point.
#include <cmath>
using namespace std;
int main() {
int n, m;
int k;
int sum = 0;
cout << "Ievadiet pirmo skaitli: "; /// 1 ///
cin >> n;
cout << "Ievadiet otro skaitli: "; /// 153 ///
cin >> m;
cout << "Ievadiet pakapi: "; /// 3 ///
cin >> k;
for(int i = n; i <= m; i++) { /// (Parbaudam katru skaitli [n,m] intervala.) (i = 153, 153 <= 153, 153 + 1)
while(i > 0) { /// (153 > 0)
int num = i % 10; /// 153 % 10 = 3 ///
cout << "Num: " << num << endl;
double pakape = pow(num, k); /// (Noapalojam ieguto skaitli.) 3^3 = 27 ///
cout << "Pakape: " << pakape << endl;
sum = sum + pakape; /// 0 = 0 + 27 ///
cout << "Sum: " << sum << endl;
i = i / 10; /// 153 / 10 = 15 ///
cout << "i: " << i << endl;
if (sum == n) { /// 27 == 153 ///
cout << sum; /// returns 1 ///
sum = 0; /// 1 - 1 = 0 ///
}
}
}
return 0;
}
It works kind of what I expect but the problem is that it's in a infinite loop and I don't know how to fix that. If anyone could help me out, I would greatly appreciate it, thanks.
If any questions, feel free to ask me.