C Program to Find Quotient and Remainder

Problem Description

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.

Problem Solution

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.

advertisement
advertisement

Method 1: (Using Division and Modulo Operator)

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:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If a=10 and b=3, when a is divided by b,

Quotient = a/b = 10/3 = 3
Remainder = a % b = 10 % 3 = 1

Program/Source Code

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.

  1. /*
  2.  * C Program to find the quotient and remainder
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     int a,b;
  10.     printf("Enter Dividend: ");
  11.     scanf("%d",&a);
  12.     printf("Enter Divisor: ");
  13.     scanf("%d",&b);
  14.  
  15.     // Calculates the quotient
  16.     int quotient = a/b;
  17.  
  18.     // Calculates the remainder
  19.     int remainder = a%b;
  20.     printf("Quotient is : %d\nRemainder is: %d",quotient,remainder);
  21.     return 0;
  22. }
Program Explanation

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.

advertisement

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).

Runtime Test Cases

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

advertisement
Method 2: (Using Loops)

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.

Program/Source Code

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 ******

  1. /*
  2.  * C Program to find the quotient and remainder using loops
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     int a,b;
  10.     printf("Enter Dividend: ");
  11.     scanf("%d",&a);
  12.     printf("Enter Divisor: ");
  13.     scanf("%d",&b);
  14.     int quotient = 0;
  15.  
  16.     //Initializing the loop
  17.     while(quotient*b <= a)
  18.       quotient++;
  19.  
  20.       //Decrementing 1 because we came one step forward
  21.       quotient--;
  22.       int remainder= a - (b * quotient); 
  23.     printf("Quotient is : %d\nRemainder is: %d",quotient,remainder);
  24.     return 0;
  25. }
Program Explanation

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).

Runtime Test Cases

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

Method 3: (Using Function)

In this approach, we create two functions which return quotient and remainder respectively.

Program/Source Code

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.

  1. /*
  2.  * C Program to find the quotient and remainder using function
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int find_quotient(int a,int b)
  8. {
  9.     //Return Quotient
  10.     return a/b;
  11. }
  12. int find_remainder(int a,int b)
  13. {
  14.     //Return Quotient
  15.     return a%b;
  16. }
  17.  
  18. int main()
  19. {
  20.     int dividend,divisor;
  21.     printf("Enter Dividend: ");
  22.     scanf("%d",&dividend);
  23.     printf("Enter Divisor: ");
  24.     scanf("%d",&divisor);
  25.     int quotient = find_quotient(dividend,divisor);
  26.     int remainder=find_remainder(dividend,divisor);
  27.     printf("Quotient: %d\nRemainder: %d",quotient,remainder);
  28.     return 0;
  29. }
Program Explanation

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).

Runtime Test Cases

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”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.