程序根据用户输入的被除数和除数查找商和余数。
示例 1:用于查找商和余数的程序
在该程序中,要求用户输入被除数和除数,然后程序根据输入值查找商和余数。
#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;}输出:
Enter dividend: 15Enter divisor: 2Quotient is: 7Remainder is: 1
示例 2:使用函数查找商和余数的程序
在这个程序中,我们正在做上述程序的相同的事情,但是在这里我们使用函数来查找商和余数。我们为计算创建了两个用户定义的函数。要理解这个程序,您应该对以下 C 编程主题有基本的了解:
#include <stdio.h>// Function to computer quotientint quotient(int a, int b){ return a / b;}// Function to computer remainderint 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 程序:
