C Math

In c we can compile variety of mathematical functions using math.h header file.
Here are some commenly used math functions
1. Trigonometric Functions - sin(x), cos(x), tan(x), asin(x), acos(x), atan(x)
2. Exponential and Logarithmic Functions - exp(x), log(x), log10(x)
3. Power Functions - pow(x, y), sqrt(x)
4. Rounding Functions - ceil(x), floor(x), round(x)
5. Absolute Value - fabs(x)
6. Random Number Generation - rand(), srand(seed)

Trigonometric Functions

Function

Description

Example

sin(x)

Sine function

sin(0.5) returns the sine of 0.5 radians

cos(x)

Cosine function

cos(0.5) returns the cosine of 0.5 radians

tan(x)

Tangent function

tan(0.5) returns the tangent of 0.5 radians

asin(x)

Arcsine function (inverse of sine)

asin(0.5) returns the arcsine of 0.5, in radians

acos(x)

Arccosine function (inverse of cosine)

acos(0.5) returns the arccosine of 0.5, in radians

atan(x)

Arctangent function (inverse of tangent)

atan(0.5) returns the arctangent of 0.5, in radians

Example

``````#include <stdio.h>
#include <math.h>

int main() {
double x = 2.5;
double y = 3.0;

// Trigonometric functions
printf("sin(%f) = %f\n", x, sin(x));
printf("cos(%f) = %f\n", x, cos(x));
printf("tan(%f) = %f\n", x, tan(x));
printf("asin(%f) = %f\n", x, asin(x));
printf("acos(%f) = %f\n", x, acos(x));
printf("atan(%f) = %f\n", x, atan(x));

return 0;
}``````

Exponential and Logarithmic Functions

Function

Description

Example

exp(x)

Exponential function (e^x)

exp(2.0) returns the value of e^2

log(x)

Natural logarithm (base e)

log(10.0) returns the natural logarithm of 10

log10(x)

Base-10 logarithm

log10(100.0) returns the base-10 logarithm of 100

Example

``````#include <stdio.h>
#include <math.h>

int main() {
double x = 2.5;
double y = 3.0;

// Exponential and logarithmic functions
printf("exp(%f) = %f\n", x, exp(x));
printf("log(%f) = %f\n", x, log(x));
printf("log10(%f) = %f\n", x, log10(x));

return 0;
}``````

Power and Rounding Functions

Function

Description

Example

pow(x, y)

Power function (x^y)

pow(2.0, 3.0) returns 2^3

sqrt(x)

Square root

sqrt(25.0) returns the square root of 25

ceil(x)

Ceil (round up to the nearest integer)

ceil(2.3) returns 3

floor(x)

Floor (round down to the nearest integer)

floor(2.9) returns 2

round(x)

Round to the nearest integer

round(2.3) returns 2, round(2.7) returns 3

Example

``````#include <stdio.h>
#include <math.h>

int main() {
double x = 2.5;
double y = 3.0;
// Power function
printf("pow(%f, %f) = %f\n", x, y, pow(x, y));

// Square root
printf("sqrt(%f) = %f\n", x, sqrt(x));

// Rounding functions
printf("ceil(%f) = %f\n", x, ceil(x));
printf("floor(%f) = %f\n", x, floor(x));
printf("round(%f) = %f\n", x, round(x));

}``````

Absolute Value and Random Number Generation

Function

Description

Example

fabs(x)

Absolute value

fabs(-5.0) returns 5

rand()

Pseudo-random integer between 0 and RAND_MAX

rand() generates a random intege

srand(seed)

Seed for rand() function

srand(123) sets the seed for reproducibility

Example

``````#include <stdio.h>
#include <math.h>

int main() {
double x = 2.5;
double y = 3.0;

// Absolute value
printf("fabs(%f) = %f\n", -x, fabs(-x));

// Random number generation
srand(123); // seed for reproducibility
printf("rand() = %d\n", rand());

}``````

Quick Recap - Topics Covered

C Math

Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.

Example js

Example 1 Example 2 Example 3 Example 4 Example 5

FEEDBACK