C Programming: Math Concepts &
Solutions
A Comprehensive Guide to Mathematical Logic in C
1. Integer Division & Modulo
Question: What is the output of printf("%d, %d", 17 / 3, 17 % 3); ?
Explanation: Integer division in C truncates the decimal. 17 divided by 3 is 5.66, which
becomes 5. The modulo operator (%) returns the remainder.
Answer: 5, 2
2. Floating Point Precision
Question: How do you calculate 5 divided by 2 to get 2.5 in C?
float result = (float)5 / 2;
Explanation: Without the (float) cast, C performs integer division (5/2 = 2). Casting one
operand to a float forces the operation to use floating-point math.
Answer: Use explicit type casting.
3. Power and Square Root
Question: Use math.h to calculate the hypotenuse of a triangle with sides 3 and 4.
#include <math.h>
double c = sqrt(pow(3, 2) + pow(4, 2));
Answer: 5.00
4. Operator Precedence
Question: Evaluate the expression: int x = 10 + 5 * 2 / 5 - 1;
Order: (5*2)=10 -> (10/5)=2 -> (10+2)=12 -> (12-1)=11.
Answer: 11
5. Increment/Decrement Logic
Question: What is the value of b ?
int a = 5; int b = ++a + a++;
Explanation: ++a (prefix) makes a=6 and returns 6. Then a++ (postfix) returns 6 and then
increments a to 7. Result: 6 + 6 = 12.
Answer: 12
6. Absolute Values
Question: Which function is used for the absolute value of a double?
Note: abs() is for integers (stdlib.h), while fabs() is for doubles (math.h).
Answer: fabs()
7. Rounding Functions
Question: What are the results of ceil(4.2) and floor(4.8) ?
Ceil rounds up to the nearest integer; Floor rounds down.
Answer: 5.0 and 4.0
8. Compound Assignment
Question: If x = 10 , what is x *= 3 + 2; ?
The expression on the right is evaluated first. x = 10 * (3 + 2) = 10 * 5.
Answer: 50
9. Logarithmic Calculations
Question: What is the difference between log() and log10() in C?
Answer: log() is natural log (base e), log10() is base 10.
10. Area of Circle Calculation
Question: Write the C expression for Area = πr² using M_PI .
double area = M_PI * radius * radius;
Answer: Requires #define _USE_MATH_DEFINES and #include <math.h>