Arithmetic operators help you do math with numbers in your program. They let you add, subtract, multiply, divide, and find remainders easily.
Arithmetic operators
result = a + b; // addition result = a - b; // subtraction result = a * b; // multiplication result = a / b; // division result = a % b; // remainder (modulus)
Use + to add, - to subtract, * to multiply, / to divide, and % to get the remainder.
Division between integers gives an integer result (it cuts off the decimal part).
int sum = 5 + 3; // sum is 8
int diff = 10 - 4; // diff is 6
int product = 7 * 2; // product is 14
int quotient = 9 / 2; // quotient is 4
int remainder = 9 % 2; // remainder is 1
This program shows how to use all the basic arithmetic operators with two numbers, 15 and 4.
#include <stdio.h> int main() { int a = 15, b = 4; printf("Addition: %d + %d = %d\n", a, b, a + b); printf("Subtraction: %d - %d = %d\n", a, b, a - b); printf("Multiplication: %d * %d = %d\n", a, b, a * b); printf("Division: %d / %d = %d\n", a, b, a / b); printf("Remainder: %d %% %d = %d\n", a, b, a % b); return 0; }
Remember that dividing integers drops the decimal part, so 15 / 4 is 3, not 3.75.
The modulus operator % only works with integers.
Be careful not to divide by zero; it will cause an error.
Arithmetic operators let you do basic math in C.
Use +, -, *, /, and % for addition, subtraction, multiplication, division, and remainder.
Division with integers cuts off decimals, and modulus gives the remainder.
Practice
Solution
Step 1: Understand the modulus operator
The modulus operator (%) returns the remainder after division of two integers.Step 2: Compare with other operators
Division (/) returns the quotient, multiplication (*) multiplies, subtraction (-) subtracts.Final Answer:
The modulus operator (%) -> Option CQuick Check:
Remainder = % operator [OK]
- Confusing division (/) with modulus (%)
- Thinking multiplication (*) gives remainder
- Using subtraction (-) for remainder
Solution
Step 1: Recall multiplication operator in C
The multiplication operator in C is the asterisk symbol (*).Step 2: Check each option
a * b; uses '*', which is correct. Options B, C, D use invalid symbols for multiplication.Final Answer:
a * b; -> Option BQuick Check:
Multiply with * operator [OK]
- Using 'x' instead of '*' for multiplication
- Using '.' or '&' mistakenly
- Missing semicolon at end
int a = 7, b = 3;
printf("%d", a / b);Solution
Step 1: Understand integer division in C
Both a and b are integers, so a / b performs integer division, which discards the decimal part.Step 2: Calculate 7 / 3 using integer division
7 divided by 3 is 2.333..., but integer division returns 2.Final Answer:
2 -> Option DQuick Check:
Integer division truncates decimals [OK]
- Expecting floating-point result
- Confusing remainder with division result
- Using wrong format specifier in printf
int x = 10;
int y = 3;
int z = x / y.0;
printf("%d", z);Solution
Step 1: Analyze the division expression
The expression x / y.0 uses y.0 which attempts a floating-point literal but causes invalid syntax, as x and y are integers.Step 2: Understand the syntax error
Using y.0 causes a compilation error due to invalid syntax in the integer division context.Final Answer:
Using a floating-point number in integer division -> Option AQuick Check:
Integer division must use integers only [OK]
- Thinking printf format is wrong
- Ignoring the decimal in y.0
- Assuming division by zero
Solution
Step 1: Understand integer vs floating-point division
Dividing by 2 (integer) causes integer division, losing decimals. Dividing by 2.0 (double) forces floating-point division.Step 2: Check each option for correct average calculation
float avg = (a + b) / 2; does integer division then converts to float, losing decimals. float avg = (a + b) / 2.0; divides by 2.0, giving float result. int avg = (a + b) / 2.0; stores float result in int, losing decimals. float avg = a / b + 2; is incorrect formula.Final Answer:
float avg = (a + b) / 2.0; -> Option AQuick Check:
Divide by 2.0 for float average [OK]
- Dividing by integer 2 causing integer division
- Storing float result in int variable
- Wrong formula for average
Ready to prove it?
Pass the test and this pattern moves unknown
