C Program to Compute Quotient and Remainder

Find quotient and remainder

The relation among divisor, dividend, quotient and remainder is dividend = divisor * quotient + remainder. C program to compute quotient and remainder between two integers can be calculated by using the division ( / ) and modulus ( % ) operators respectively. To compute the remainder of the division of two floating-point numbers, the library function fmod( ) is used. This function considers quotient as an integer number and the remainder as a floating-point number.

  The following C programs compute the quotient and remainder of two numbers. The algorithm, pseudocode and time complexity of the program have also been mentioned. The flowchart of the problem has been shown here.






1. Algorithm to Compute Quotient and Remainder


1. Take two integers say $x,~y$ as input.

2. Compute the quotient $q= floor(\frac{x}{y})$

3. Compute the remainder $r= x \mod y$

4. Return $q, r$




2. Pseudocode to Compute Quotient and Remainder


Input : Two integer numbers $x,~y$

Output : Two integer numbers as quotient and remainder when $x$ is divided by $y$

1. Procedure quotientRemainder($x, y$):

2. $q \leftarrow floor(\frac{x}{y})$

3. $r \leftarrow x \mod y$

4. Return $q,~r$

5. End Procedure





3. Time complexity to Compute Quotient and Remainder


Time Complexity: O(1)





4. C Program & output to Compute Quotient and Remainder

Code has been copied
/******************************************
            Alphabetacoder.com
C program to compute quotient and remainder
*******************************************/
#include <stdio.h>

int main() {
    //initialize two variables
    int a = 17, b = 5;
    int q, r;

    //find the quotient
    q = a / b;
    //find the remainder
    r = a % b;

    printf("When %d is divided by %d, quotient is %d and remainder is %d.", a, b, q, r);
    return 0;
}

Output


When 17 is divided by 5, quotient is 3 and remainder is 2.




5. C Program & output to Compute Remainder using fmod() function

Code has been copied
/**********************************
        Alphabetacoder.com
C program to compute the remainder
using fmod() function
***********************************/
#include <stdio.h>
#include <math.h>

int main() {
    //initialize two variables
    float a = 17.5, b = 3.7;
    float r;

    //find the remainder using fmod() function
    r = fmod(a, b);

    printf("Remainder is %f", r);
    return 0;
}

Output


Remainder is 2.700000