在这个 C++ 程序中,我们将学习如何找到商和余数。
商 = 被除数 / 除数
余数 = 被除数 % 除数。
例如 32 / 6
其中,

步骤 1: 调用头文件 iostream.
步骤 2: 使用 namespace std.
步骤 3: 打开整数类型的主函数; int main().
步骤 4: 声明整数变量;dividend, divisor, quotient, remainder;
步骤 5: 打印一条消息以输入被除数。
步骤 6: 将数字读入变量 dividend。
步骤 7: 打印一条消息以输入除数。
步骤 8: 将数字读入变量 divisor。
步骤 9: 计算 quoient = dividend / divisor; remainder = dividend % divisor;
步骤 10: 打印商和余数
步骤 11: 退出。
#include <iostream>
using namespace std;
int main()
{
int divisor, dividend, quotient, remainder;
cout << "Enter dividend: ";
cin >> dividend;
cout << "Enter divisor: ";
cin >> divisor;
quotient = dividend / divisor;
remainder = dividend % divisor;
cout << "Quotient = " << quotient << endl;
cout << "Remainder = " << remainder;
return 0;
}
Run 1 ----------- Enter dividend: 45 Enter divisor: 5 Quotient = 9 Remainder = 0 Run 2 --------- Enter dividend: 96 Enter divisor: 7 Quotient = 13 Remainder = 5