Итак, я пытаюсь создать программу, используя цикл while, чтобы найти наибольший общий делитель двух чисел. Это то, что я придумал. Однако из того, что я могу сказать, программа просто пропускает цикл полностью, когда я запускаю его. (операторы остаются 0, делитель всегда возвращается равным num1). Кто-нибудь, кто может помочь новичкам?Почему этот цикл не работает?
/* Define variables for divisors and number of operations */
int num1, num2, divisor, opers;
opers = 0;
/* Prompt user for integers and accept input */
cout << "Please enter two integers with the smaller number first, separated by a space. ";
cout << endl;
cin >> num1 >> num2;
/* Make divisor the smaller of the two numbers */
divisor = num1;
/* While loop to calculate greatest common divisor and number of calculations */
while ((num1 % divisor != 0) && (num2 % divisor != 0))
{
divisor--;
opers++;
}
/* Output results and number of calculations performed */
cout << "The greatest common divisor of " << num1 << " and " << num2 << " is: ";
cout << divisor << endl << "Number of operations performed: " << opers;
Я рекомендую вам научиться проходить через код с помощью отладчика. – StackedCrooked