PART A · PRESCRIBED EXERCISE 03
Demonstrate six math.h functions
Understand the algorithm. Run the code.
Explain the result with confidence.
LAB A03 · 01
Understand the task.
Aim: Demonstrate six math.h functions.
Mathematical library. math.h declares standard C mathematical functions. Their floating-point arguments and results support operations such as square root, powers, rounding and logarithms.
Review the related theory in Unit 1.
LAB A03 · 02
Know what goes in and what comes out.
One positive number x, greater than 0 and at most 100. This shared range keeps all six demonstrated operations in their intended domains.
Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.
LAB A03 · 03
Plan the steps before writing code.
- Read x as double and validate 0 < x <= 100.
- Evaluate sqrt(x) and pow(x, 2.0).
- Evaluate ceil(x), floor(x) and fabs(-x).
- Evaluate log(x), the natural logarithm.
- Display each result to two decimal places.
LAB A03 · 04
One complete program. Ready to run.
#include <stdio.h>
#include <math.h>
int main(void)
{
double x;
if (scanf("%lf", &x) != 1 || !(x > 0.0 && x <= 100.0)) {
puts("Enter a positive value up to 100.");
return 1;
}
printf("sqrt = %.2f\n", sqrt(x));
printf("pow(x, 2) = %.2f\n", pow(x, 2.0));
printf("ceil = %.2f\n", ceil(x));
printf("floor = %.2f\n", floor(x));
printf("fabs(-x) = %.2f\n", fabs(-x));
printf("log = %.2f\n", log(x));
return 0;
}
sqrt = 1.58
pow(x, 2) = 6.25
ceil = 3.00
floor = 2.00
fabs(-x) = 2.50
log = 0.92
Compile on your own computer
gcc A03-math-functions.c -o program -lmRun the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.
LAB A03 · 05
Connect each statement to its purpose.
- sqrt finds the nonnegative square root; pow computes a power.
- ceil rounds toward positive infinity, while floor rounds toward negative infinity. Both return floating-point values.
- fabs computes floating-point absolute value; integer abs belongs to stdlib.h.
- log means the natural logarithm, base e. log10 is a different function.
- Include math.h for declarations. For a GCC command-line build, put -lm after the source/object file to link the mathematics library on systems that require it.
LAB A03 · 06
Follow the sample calculation.
| Step or component | What happens |
|---|---|
| sqrt(2.5) | ≈ 1.58 |
| pow(2.5, 2.0) | 6.25 |
| ceil / floor | 3.00 / 2.00 |
| fabs(-2.5) | 2.50 |
| log(2.5) | ≈ 0.92 |
LAB A03 · 07
Check more than one input.
| Try this input or case | Expected behaviour |
|---|---|
| 1 | Square root 1, square 1, natural log 0. |
| 4 | Square root 2, square 16. |
| 0, negative, NaN or text | Rejected by the shared positive-input rule. |
Time and storage
A fixed six library calls; operation count is independent of the amount of input. Internal library algorithms are implementation-dependent.
Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.
LAB A03 · 08
Use the real C compiler.
Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.
Load the compiler when you’re ready to practise.
Your C workspace
Edit real C code and see real compiler output.
Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.
View / copy the selected source
#include <stdio.h>
#include <math.h>
int main(void)
{
double x;
if (scanf("%lf", &x) != 1 || !(x > 0.0 && x <= 100.0)) {
puts("Enter a positive value up to 100.");
return 1;
}
printf("sqrt = %.2f\n", sqrt(x));
printf("pow(x, 2) = %.2f\n", pow(x, 2.0));
printf("ceil = %.2f\n", ceil(x));
printf("floor = %.2f\n", floor(x));
printf("fabs(-x) = %.2f\n", fabs(-x));
printf("log = %.2f\n", log(x));
return 0;
}
LAB A03 · 09
Explain why the program works.
Does ceil return int?
No. The ceil function used here returns double. Formatting it with %.2f is valid.
Why reject zero although sqrt(0) is valid?
This same input is also passed to log, whose intended finite real-valued domain here is positive numbers.
Is pow(x, 2.0) the same as x ^ 2?
No. In C, ^ is the bitwise XOR operator, not exponentiation.
LAB A03 · 10
Build the next skill.
Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.