#Loop

18 messages · Page 1 of 1 (latest)

north stag
#

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.

pine grailBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

strong shardBOT
# north stag My goal is to create a code that will go through number list and search for arms...
#include <cmath>
#include <iostream>

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;
}

```Requested by: aj00#7487
thorn pumice
#
#include <cmath>
#include <iostream>

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;
}```
north stag
#

n = first number, m = second number, k = power number

thorn pumice
#

what is an astro number?

north stag
#

you mean armstrong?

thorn pumice
#

*armstrong

north stag
#

153 = 1^3 + 5^3 + 3^3

#

like the sum matches the number in specific powers

thorn pumice
#

dosn't sum need resetting after a failed attempt?

#

and also you are resetting i to 0 after every iteration

#

that should be the source of the error

#

@north stag

north stag
#

i did try with sum = sum - sum however the loop is still going

#

i get the number i want but it doesnt stop although it should loop only while i > 0

thorn pumice
#

It is guaranteed that i 0 when exiting the loop and therefore it is an infinite loop

pine grailBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.