Challenge: Quotient and Remainder
Completed
Write void divide(int a, int b, int *quotient, int *remainder) that fills in both results. Read a and b (b is never 0), call it, and print Q R.
Input
Two integers a and b.
Output
The quotient and remainder separated by a space.
Example
Input
17 5
Output
3 2
Editor
C
Hints
Inside the function, write the results with *quotient = ... and *remainder = ...
In main, pass &q and &r.
Solution explanation
Passing addresses lets one function hand back two answers.
Show solution
#include <stdio.h>
void divide(int a, int b, int *quotient, int *remainder) {
*quotient = a / b;
*remainder = a % b;
}
int main() {
int a, b, q, r;
scanf("%d %d", &a, &b);
divide(a, b, &q, &r);
printf("%d %d\n", q, r);
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module