计算商和余数的 C 程序

要理解此示例,您应该具备以下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;
}

输出

Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1

在这个程序中,要求用户输入两个整数。它们分别存储在变量中 dividenddivisor

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

然后使用 / (除法运算符)计算商,并存储在 quotient.

quotient = dividend / divisor;

类似地,余数使用 % (模运算符)计算并存储在 remainder.

remainder = dividend % divisor;

最后,使用 printf( ) 显示商和余数。

printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);