# C 程序：查找商和余数

> 原文： [https://beginnersbook.com/2017/09/c-program-to-find-quotient-and-remainder/](https://beginnersbook.com/2017/09/c-program-to-find-quotient-and-remainder/)

程序根据用户输入的被除数和除数查找**商**和**余数**。

## 示例 1：用于查找商和余数的程序

在该程序中，要求用户输入被除数和除数，然后程序根据输入值查找商和余数。

```c
#include <stdio.h>
int main(){
   int num1, num2, quot, rem;

   printf("Enter dividend: ");
   scanf("%d", &num1);

   printf("Enter divisor: ");
   scanf("%d", &num2);

   /* The "/" Arithmetic operator returns the quotient
    * Here the num1 is divided by num2 and the quotient
    * is assigned to the variable quot
    */
   quot = num1 / num2;

   /* The modulus operator "%" returns the remainder after
    * dividing num1 by num2.
    */
   rem = num1 % num2;

   printf("Quotient is: %d\n", quot);
   printf("Remainder is: %d", rem);

   return 0;
}
```

输出：

```c
Enter dividend: 15
Enter divisor: 2
Quotient is: 7
Remainder is: 1
```

## 示例 2：使用函数查找商和余数的程序

在这个程序中，我们正在做上述程序的相同的事情，但是在这里我们使用函数来查找商和余数。我们为计算创建了两个用户定义的函数。要理解这个程序，您应该对以下 [C 编程](https://beginnersbook.com/2014/01/c-tutorial-for-beginners-with-examples/)主题有基本的了解：

1.  [C 中的函数](https://beginnersbook.com/2014/01/c-functions-examples/)
2.  [C 中的按值函数调用](https://beginnersbook.com/2014/01/c-function-call-by-value-example/)

```c
#include <stdio.h>
// Function to computer quotient
int quotient(int a, int b){
   return a / b;
}

// Function to computer remainder
int remainder(int a, int b){
   return a % b;
}

int main(){
    int num1, num2, quot, rem;
    printf("Enter dividend: ");    
    scanf("%d", &num1);
    printf("Enter divisor: ");    
    scanf("%d", &num2);

    //Calling function quotient()    
    quot = quotient(num1, num2);

    //Calling function remainder()    
    rem = remainder(num1, num2);
    printf("Quotient is: %d\n", quot);    
    printf("Remainder is: %d", rem);
    return 0;
}
```

查看相关的 [C 程序](https://beginnersbook.com/2015/02/simple-c-programs/)：

1.  [C 程序：相加两个数字](https://beginnersbook.com/2017/09/c-program-to-add-two-numbers/)
2.  [C 程序：相乘两个浮点数](https://beginnersbook.com/2017/09/c-program-to-multiply-two-floating-point-numbers/)
3.  [C 程序：查找字符的 ASCII 值](https://beginnersbook.com/2017/09/c-program-to-find-ascii-value-of-a-character/)
4.  [C 程序：计算并打印 nCr](https://beginnersbook.com/2015/02/c-program-to-calculate-and-print-the-value-of-ncr/) 的值