商和余数与除数和除数一起是除法的一部分。
我们除以的数称为股息。除数的数字称为除数。除法后获得的结果称为商,剩余的数为余数。
dividend = divisor * quotient + remainder
例如:如果15除以7,则2是商,而1是余数。在这里,15是红利,7是除数。
15 = 7 * 2 + 1
查找商和余数的程序如下:
#include <iostream> using namespace std; int main() { int divisor, dividend, quotient, remainder; dividend = 15; divisor = 7; quotient = dividend / divisor; remainder = dividend % divisor; cout << "Dividend is " << dividend <<endl; cout << "Divisor is " << divisor <<endl; cout << "Quotient is " << quotient << endl; cout << "Remainder is " << remainder; return 0; }
输出结果
Dividend is 15 Divisor is 7 Quotient is 2 Remainder is 1
在上述程序中,商是通过将除数除以除数来获得的。余数通过使用除数和除数的模运算符获得。
quotient = dividend / divisor; remainder = dividend % divisor;
之后,将显示除数,除数,商和余数。
cout << "Dividend is " << dividend <<endl; cout << "Divisor is " << divisor <<endl; cout << "Quotient is " << quotient << endl; cout << "Remainder is " << remainder;