简体   繁体   中英

Codeblocks Project.exe stopped responding c++

I am working on a simple algorithm for the gcd of two numbers. I don't know why it crashes when I finished entering a and b.

Here's the code:

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
    int a, b, d, i, max;
    cin >> a;
    cin >> b;
    if (a<b)
    {
        max = b;
    }
    else
    {
        max = a;
    }

    for (i = 0; i <= max; i++)
    {
        if (a%i == 0 && b%i == 0)
        {
            d = i;
        }
    }
    cout << d << endl;
}

problem is : a%i == 0 && b%i == 0 when i=0 . this code works fine now :

#include #include

using namespace std;

int main()
{
    int a, b, d, i, max;
    cin >> a;
    cin >> b;
    if (a<b)
    {
        max = b;
    }
    else
    {
        max = a;
    }

    for (i = 1; i <= max; i++)
    {
        if (a%i == 0 && b%i == 0)
        {
            d = i;
        }
    }
    cout << d << endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM