Write a C program to find quotient and remainder of two numbers.
Quotient: The result of dividing one integer by another is the quotient. For example, if we divide 9 by 3, the result is 3, which is the quotient.
Remainder: The value remaining after division is called the remainder. If one number (the divisor) is not divisible by another (the divisor) then the division gives a value. For example, when 10 is divided by 3, 1 is left as remainder.
When a is divided by b, quotient is the maximum number which when multiplied by b, will result in a number less than equal to a. Rmainder is calculated as dividend – (divisor)*(quotient).
Example:
If a = 11 and b = 3, when a is divided by b, i.e 11/3
Quotient = 3 and Remainder = 2
There are several ways to find the quotient and remainder in C language. Let’s take a detailed look at all the approaches to find the quotient and remainder.
- Find Quotient and Remainder in C using Division and Modulo Operator
- Find Quotient and Remainder in C using Loops
- Find Quotient and Remainder in C using Function
In this method, we take user input to enter the dividend and divisor values, and then the program finds the quotient and remainder depending on the input values.
Division Operator: Returns quotient
Modulo Operator: Returns remainder
Example:
If a=10 and b=3, when a is divided by b,
Quotient = a/b = 10/3 = 3
Remainder = a % b = 10 % 3 = 1
Here is source code of the C Program to find the quotient and remainder. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C Program to find the quotient and remainder
*/
#include <stdio.h>
int main()
{
int a,b;
printf("Enter Dividend: ");
scanf("%d",&a);
printf("Enter Divisor: ");
scanf("%d",&b);
// Calculates the quotient
int quotient = a/b;
// Calculates the remainder
int remainder = a%b;
printf("Quotient is : %d\nRemainder is: %d",quotient,remainder);
return 0;
}
1. First, take two values as input from the user and store it in variable a and b.
2. Then find quotient using the division operator i.e quotient = a/b and store the value in quotient variable.
3. Now finds the remainder value using the modulo operator i.e remainder = a%b and store the value in remainder variable.
4. Print the quotient and remainder values.
Time Complexity: O(1)
In the above program there are no iterative statements, no function calls, the only operation performed is input output, so time complexity is O(1)
Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).
Here is the runtime output of the C program to find the quotient and remainder when the user entered Dividend = “10” and Divisor = “3”.
Enter Dividend: 10 Enter Divisor: 3 Quotient is : 3 Remainder is: 1
In this approach we iterate until the multiplication of the divisor with a number ‘a’ is less than equal to the dividend. After iterations are completed, a is the quotient and remainder is:
Remainder = Dividend – (divisor)*quotient;
Example:
If a = 11 and b = 3, when a is divided by b, i.e. 11/3 = 3.
Quotient = 3;
Remainder = Dividend – (divisor)*quotient;
Remainder = 11 – (3*3) = 11 – 9 = 2.
Here is source code of the C Program to find the quotient and remainder using loops. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
****** Following program works only for positive integers ******
/*
* C Program to find the quotient and remainder using loops
*/
#include <stdio.h>
int main()
{
int a,b;
printf("Enter Dividend: ");
scanf("%d",&a);
printf("Enter Divisor: ");
scanf("%d",&b);
int quotient = 0;
//Initializing the loop
while(quotient*b <= a)
quotient++;
//Decrementing 1 because we came one step forward
quotient--;
int remainder= a - (b * quotient);
printf("Quotient is : %d\nRemainder is: %d",quotient,remainder);
return 0;
}
1. First, take two values as input from the user and store it in variable a and b.
2. Initiate while loop until divisor multiplied by quotient is less than dividend. In each iteration increment quotient.
3. After iterations are completed, remainder is calculated as dividend – (quotient * divisor)
4. Print the quotient and remainder values.
Time Complexity: O(n)
As a loop is executed, the time complexity of the above program is linear i.e O(n).
Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).
Here is the runtime output of the C program to find the quotient and remainder when the user entered Dividend = “11” and Divisor = “3”.
Enter Dividend: 11 Enter Divisor: 3 Quotient is : 3 Remainder is: 2
In this approach, we create two functions which return quotient and remainder respectively.
Here is source code of the C Program to find the quotient and remainder using function. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C Program to find the quotient and remainder using function
*/
#include <stdio.h>
int find_quotient(int a,int b)
{
//Return Quotient
return a/b;
}
int find_remainder(int a,int b)
{
//Return Quotient
return a%b;
}
int main()
{
int dividend,divisor;
printf("Enter Dividend: ");
scanf("%d",÷nd);
printf("Enter Divisor: ");
scanf("%d",&divisor);
int quotient = find_quotient(dividend,divisor);
int remainder=find_remainder(dividend,divisor);
printf("Quotient: %d\nRemainder: %d",quotient,remainder);
return 0;
}
1. First, take two values as input from the user and store it in variable dividend and divisor.
2. Create two functions to find quotient using the division operator and remainder using the modulo operator.
3. Print value returned by both functions.
Time Complexity: O(1)
In the above program there are no iterative statement, the only operation performed is input output, so time complexity is O(1).
Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).
Here is the runtime output of the C program to find the quotient and remainder when the user entered Dividend = “23” and Divisor = “5”.
Enter Dividend: 23 Enter Divisor: 5 Divident: 4 Remainder: 3
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Buy C Books
- Apply for C Internship