Ho iniziato questo programma per calcolare il massimo comun divisore. Questo è quello che ho finora:Programma C++ per calcolare il massimo comun divisore
#include <iostream>
#include <math.h>
using namespace std;
int getGCD(int a, int b)
{
a = a % b;
if (a == 0)
{
return b;
b = b % a;
}
if (b == 0)
{
return a;
}
}
int main()
{
int x, y;
cout << "Please enter two integers x and y, for GCD calculation" << endl;
cin >> x >> y;
cout << "The GCD of " << x << "and " << y << " is" << getGCD(x, y) << endl;
return 0;
}
ho sempre ottenere un 0 per il GCD. Che cosa sto facendo di sbagliato?
b = b% a; non eseguirà mai – Mikhail
controlla la riga return b; e chiediti, come può il programma eseguire b = b% a; se l'hai detto prima di uscire da questa funzione. – dowhilefor
se questo è compito, dovresti aggiungere il tag appropriato :) –