We can calculate the quotient and remainder of two numbers in C by utilizing the division operator / to obtain the quotient and the modulus operator % to ascertain the remainder.
In this code snippet, users are prompted to input two numbers to calculate the quotient and remainder. Subsequently, the scanf function is employed for input retrieval, storing the values in the variables var1 and var2. By utilizing the division operator / and modulo operator %, the variables var1 and var2 are divided to obtain the quotient and remainder, which are then saved in the variables quotient and remaining.
#include <stdio.h>
int main()
{
int var_1, var_2, Quotient = 0, Remainder = 0;
printf("Enter the first number: \n");
scanf("%d", &var_1);
printf("Enter the second number: \n");
scanf("%d", &var_2);
Quotient = var_1 / var_2;
Remainder = var_1 % var_2;
printf("Quotient when var_1/var_2 is: %d\n", Quotient);
printf("Remainder when var_1/var_2 is: %d", Remainder);
return 0;
}
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 Loops
In this instance, the C code employs looping structures to determine the quotient, with the remainder displayed subsequently:
#include <stdio.h>
int main()
{
int var_1, var_2;
int Quotient=0, Remainder=0;
printf("Enter the first number:\n");
scanf("%d", &var_1);
printf("Enter the second number:\n");
scanf("%d", &var_2);
while (Quotient * var_2 <= var_1)
Quotient++;
Quotient--;
Remainder = var_1 - (var_2 * Quotient);
printf("Quotient when var_1/var_2 is: %d\n", Quotient);
printf("Remainder when var_1/var_2 is: %d", Remainder);
return 0;
}
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, var1, var2, 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 (var1) and the second number (var2) , 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 var2 = var1 . This loop seeks to identify the greatest integer Quotient such that Quotient var2 is less than or equal to var1 .
- 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 var2 * Quotient from var1 . 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 Functions
The upcoming program demonstrates the utilization of functions to determine both the quotient and remainder of a given number:
#include <stdio.h>
int Find_Quotient(int n, int m)
{
return n / m;
}
int Find_Remainder(int n, int m)
{
return n % m;
}
int main()
{
int var_1,var_2;
int Quotient=0,Remainder=0;
printf("Enter the first number:\n");
scanf("%d", &var_1);
printf("Enter the second number:\n");
scanf("%d", &var_2);
Quotient = Find_Quotient(var_1,var_2);
Remainder = Find_Remainder(var_1,var_2);
printf("Quotient when var_1/var_1 is: %d\n", Quotient);
printf("Remainder when var_1/var_2 is: %d", Remainder);
return 0;
}
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 FindQuotient and FindRemainder 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 var1, var2, 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 ( var1 and var2 ), and the scanf function is used to receive these values from the user.
- It uses the var1 and var2 inputs to run the Find_Quotient function, which then returns the quotient and assigns the value to the Quotient
- It uses the FindRemainder function with the arguments var1 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.