This program uses a division operator on two input numbers. The output is a quotient and a remainder is printed to the console.
We compiled the program using Turbo C++ 3.0 running in a DOS Box 0.74 on a Windows 7 64-bit system. You may try the program using a different standard C compiler, but make necessary changes to the source code.
Learn the following concepts before you try this example program.
Problem Definition
When the program runs, it ask for two input numbers, a and b. The number a is dividend and b is divisor for the program which uses division operator (/) to divide the numbers and return the results.
The program performs the simple division as follows,
\begin{aligned} &q = a/b\\ \\ &r = a % b \end{aligned}
Where a and b are two integer values and q is quotient and r is remainder values respectively.
Flowchart – Program to find Quotient and Remainder
Program Code – Remainder and Quotient
#include <stdio.h>
#include <conio.h>
main()
{
int dnd, div, quotient, remainder;
int i;
//Enter Input Numbers 'a' and 'b'
printf ("Enter the dividend:");
scanf ("%d", &dnd);
printf ("Enter the divisor :");
scanf ("%d",&div);
//Compute Quotient and Remainder
quotient = dnd/div;
remainder = dnd % div;
//Print the Results
for(i=0;i < 20;i++)
printf("_");printf("\n\n");
printf ("Quotient = %d\n", quotient);
printf ("Remainder= %d\n", remainder);
for(i=0;i<20;i++)
printf("_");printf("\n");
getch ();
return 0;
}
The most important code in the above program is as follows.
\begin{aligned}& quotient = dnd/div \\ \\ &remainder = dnd \hspace{1mm} \% \hspace{1mm}div; \end{aligned}
The values of quotient and remainder is printed to console before the program terminates.
Output
In the results below, 34 is divided by 6 and results in quotient = 5 and remainder = 4 because
\begin{aligned}& a = q * b + r \\ \\ &34 = 5 * 6 + 4\\ \\ &= 30 + 4 \end{aligned}
Enter the dividend:34
Enter the divisor :6
_____________________
Quotient = 5
Remainder= 4
_____________________