C++ 程序查找商和余数


商和余数是除法的一部分,还有被除数和除数。

我们要除的数字称为被除数。除被除数的数字称为除数。除法后的结果称为商,剩下的数字为余数。

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;

更新于: 2020 年 6 月 23 日

10K+ 浏览量

开启您的 职业生涯

完成课程可获得认证

开始
广告
© . All rights reserved.