さて、私はwhileループを使用してプログラムを作成し、2つの数値の最大公約数を見つけようとしています。これが私が思いついたものです。しかし、私が知る限り、プログラムを実行すると、プログラムはループを完全にスキップしているように見えます。(opersは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;