C program to compute quotient and remainderIn this article, we will discuss a C program to compute quotient and remainder with different methods. 1. Utilizing the division and modulo operatorsWe can determine the quotient and remainder of two numbers in C using the division operator / to get the quotient and the modulus operator % to determine the remainder. In the program below, the user is first required to enter two numbers to get the quotient and remainder of the two numbers. After that, the scanf() function is used to scan the inputs, and the results are stored in the variables var_1 and var_2. Using the arithmetic operators / and % to produce the quotient and remainder, the variables var_1 and var_2 are divided with the results being stored in the variables quotient and remaining. Output: Enter the first number:
10
Enter the second number:
2
Quotient when var_1/var_2 is: 5
Remainder when var_1/var_2 is: 0
2. Applying LoopsIn this example, the C program uses loops to find the quotient, and the remainder is shown below: Output: Enter the first number:
10
Enter the second number:
2
Quotient when var_1/var_2 is: 5
Remainder when var_1/var_2 is: 0
Explanation: - In this example, the main function is defined at the beginning of the program, followed by the inclusion of the standard input/output library (h).
- It declares four integer variables, var_1, var_2, Quotient, and Remainder, to hold user input and the calculated quotient and remainder.
- The program uses the printf() function to ask the user to enter the first number (var_1) and the second number (var_2), and the scanf() function is used to receive these values from the user.
- After that, a while loop is inserted into the code. The loop's test is Quotient * var_2 = var_1. This loop seeks to identify the greatest integer Quotient such that Quotient * var_2 is less than or equal to var_1.
- The Quotient is increased by 1 after each iteration of the loop until the condition is no longer met.
- The quotient is reduced by 1 after the loop closes. It is because the Quotient would have exceeded the biggest integer quotient that satisfies the criterion by one after the loop's final iteration. So, it is fixed by deducting 1.
- The code determines the right Quotient. After that, it determines the Remainder by deducting var_2 * Quotient from var_1. The remainder of the division is calculated in this phase.
- After that, the computed quotient and remainder are printed using printf by the program.
3. Using FunctionsThe following is a program that uses functions to find the quotient and remainder of a number: Output: Enter the first number:
10
Enter the second number:
2
Quotient when var_1/var_1 is: 5
Remainder when var_1/var_2 is: 0
Explanation: - In this example, the Find_Quotient and Find_Remainder functions are defined at the beginning of the program, which also includes the standard input/output library (h). The quotient and remainder of n divided by m are the results of these functions, which take two integer inputs, n and m.
- The integer variables var_1, var_2, Quotient, and Remainder are declared inside the main function. These variables will hold both the calculated quotient and remainder, as well as user input.
- The program uses the printf() function to ask the user to enter the first and second numbers (var_1 and var_2), and the scanf() function is used to receive these values from the user.
- It uses the var_1 and var_2 inputs to run the Find_Quotient function, which then returns the quotient and assigns the value to the Quotient
- It uses the Find_Remainder function with the arguments var_1 and var_2 to calculate the remaining and then assigns the result to the remaining variable.
- After that, the computed quotient and remainder are printed using t by the program.
|