好的,所以我試圖用while循環創建一個程序來查找兩個數字的最大公約數。這是我想出的。但是,從我所知道的情況來看,當我運行它時,程序似乎完全跳過了循環。 (操作符保持爲0,除數總是等於num1)。任何人都可以幫助新手?這個while循環爲什麼不起作用?
/* 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 2009-10-22 15:10:18