计算商数和余数的程序
#include
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", ÷nd);
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
在此程序中,要求用户输入两个整数(红利和除数)。它们分别存储在变量prise和divisor中 。
printf("Enter dividend: ");
scanf("%d", ÷nd);
printf("Enter divisor: ");
scanf("%d", &divisor);
然后使用/
(除法运算符 )评估商,并将其存储在商中 。
quotient = dividend / divisor;
同样,余数使用%
(取模运算符)求值并存储在余数中 。
remainder = dividend % divisor;
最后,使用printf(
)
显示商和余数。
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
了解有关运算符如何在C编程中工作的更多信息。