ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# C 程序:商和余数 > 原文: [https://www.programiz.com/c-programming/examples/remainder-quotient](https://www.programiz.com/c-programming/examples/remainder-quotient) #### 在此示例中,您将学习找到一个整数除以另一个整数时的商和余数。 要理解此示例,您应该了解以下 [C 编程](/c-programming "C tutorial")主题: * [C 数据类型](/c-programming/c-data-types) * [C 变量,常量和字面值](/c-programming/c-variables-constants) * [C 输入输出(I/O)](/c-programming/c-input-output) * [C 编程运算符](/c-programming/c-operators) * * * ## 计算商数和余数的程序 ```c #include <stdio.h> int main() { int dividend, divisor, quotient, remainder; printf("Enter dividend: "); scanf("%d", &dividend); printf("Enter divisor: "); scanf("%d", &divisor); // Computes quotient quotient = dividend / divisor; // Computes remainder remainder = dividend % divisor; printf("Quotient = %d\n", quotient); printf("Remainder = %d", remainder); return 0; } ``` **输出** ```c Enter dividend: 25 Enter divisor: 4 Quotient = 6 Remainder = 1 ``` * * * 在此程序中,要求用户输入两个整数(被除数和除数)。 它们分别存储在变量`dividend`和`divisor`中。 ```c printf("Enter dividend: "); scanf("%d", &dividend); printf("Enter divisor: "); scanf("%d", &divisor); ``` 然后使用`/`(除法运算符)求值商,并将其存储在`quotient`中。 ```c quotient = dividend / divisor; ``` 类似地,使用`%`(模运算符)求值余数,并将其存储在`remainder`中。 ```c remainder = dividend % divisor; ``` 最后,使用`printf()`显示商和余数。 ```c printf("Quotient = %d\n", quotient); printf("Remainder = %d", remainder); ``` 了解有关[运算符如何在 C 编程中工作的更多信息](/c-programming/c-operators "C Operators")。