Use arithmetic operators and control integer division.
Understand the concept
Division between two integers truncates the fractional part toward zero. Converting an operand to double before division preserves a fractional result. The remainder operator % works on integers. Parentheses make evaluation order explicit.
Read the example
The complete program appears below. Read it first, predict the output, then run it.
Expected output
Integer: 3
Decimal: 3.5
Remainder: 1
Practise
Use a = 17 and b = 5, predict all three results and check them.
Common mistake
Never divide by zero. Assigning integer division to double does not recover the lost fraction.
#include <stdio.h>
int main(void) {
int a = 7, b = 2;
printf("Integer: %d\n", a / b);
printf("Decimal: %.1f\n", (double)a / b);
printf("Remainder: %d\n", a % b);
return 0;
}
PRACTICE LAB
Read. Edit. Run.
C source · .c
Experiment with the program below. Your code and input are sent to the selected compiler only when you run it.
Tab indents · Shift+Tab unindents · Esc then Tab leaves the editor · Ctrl/Cmd+Enter runs · Ctrl/Cmd+S saves a draft
Changes save automatically to your account.
Input / output example
Example input
No input required.
Expected output for the original program
Integer: 3
Decimal: 3.5
Remainder: 1
Output will appear here.
Debugging Lab
Interactive Memory Visualizer
Educational simulation — addresses and execution steps are illustrative.