C++ 实例 – 求商及余数
最后更新于:2022-03-27 02:33:42
C++ 实例 – 求商及余数
使用 C++ 获取用户的输入两个数字,并将两个数字相除,然后将商和余数输出到屏幕:
实例
#include <iostream>
using namespace std; int main()
{
int divisor, dividend, quotient, remainder;
cin >> dividend;
cin >> divisor;
remainder = dividend % divisor;
cout << "余数 = " << remainder;
}
using namespace std; int main()
{
int divisor, dividend, quotient, remainder;
cout << "输入被除数: ";
cin >> dividend;
cout << "输入除数: ";
cin >> divisor;
quotient = dividend / divisor;
remainder = dividend % divisor;
cout << "商 = " << quotient << endl;
cout << "余数 = " << remainder;
return 0;
}
以上程序执行输出结果为:
输入被除数: 13 输入除数: 4 商 = 3 余数 = 1