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;
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP