C Math Functions

There are various math methods.h header file. The commonly used functions of math.h header files are given below.

Here are some examples of common math function categories in C:

No. Function Description
1) ceil(number) rounds up the given number. It returns the integer value which is greater than or equal to given number.
2) floor(number) rounds down the given number. It returns the integer value which is less than or equal to given number.
3) sqrt(number) returns the square root of given number.
4) pow(base, exponent) returns the power of given number.
5) abs(number) returns the absolute value of given number.

C Math Example

Let's see a simple example of math functions found in math.h header file.

Example

Example

#include<stdio.h>
#include <math.h>
#include <stdlib.h>
int main(){  
printf("\n%f",ceil(3.6));  
printf("\n%f",ceil(3.3));  
printf("\n%f",floor(3.6));  
printf("\n%f",floor(3.2));  
printf("\n%f",sqrt(16));  
printf("\n%f",sqrt(7));  
printf("\n%f",pow(2,4));  
printf("\n%f",pow(3,3));  
printf("\n%d",abs(-12));   
 return 0;  
}

Output:

Output

4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12

Trigonometric Functions:

The trigonometric functions are also used in the math function. The sine, cosine , and tangent of an angle are calculated using the functions sin, cos, and sin .

Other known mathematical functions besides trigonometric functions are:

  • exp: returns a number's exponential value (ex).
  • log calculates a number's natural logarithm (base e).
  • log10 computes a number's common logarithm (base 10).
  • pow raises a number to a given power.
  • Rounding Functions:

Following are some rounding functions that used in math function:

  • ceil rounds a number up to the nearest integer.
  • floor reduces a number to the nearest integer.
  • round returns the nearest integer to a floating-point number.
  • Other Mathematical Functions:

Following are some other mathematical functions that used in math function:

  • sqrt calculates the square root of a number.
  • fabs returns the absolute value of a number.
  • fmod calculates the remainder of the division between two numbers.
  • <math.h> Header:

If you want to utilize these arithmetic functions in your C application, include the <math.h> header at the start of your source code. The header file provides the function prototypes and the math function definitions. You can use the following directive to incorporate the math. In your C application, include the following header: h>

Example

#include <math.h>

After including this header, you can utilize the math functions in your program as necessary.

Example

Example

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

int main() {
double x = 2.0;
double result = sqrt(x);
printf("The square root of %.2f is %.2f\n", x, result);
return 0;
}

Output:

Output

The square root of 2.00 is 1.41

Explanation:

In this example, the < math.h> header lets you use the sqrt function to calculate the square root of a value.

Note: Remember the potential precision difficulties in floating-point computations while working with math functions that generate floating-point numbers.

1. sqrt - Square Root Function

sqrt function determines an integer's square root.

Example

Example

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

int main() {
double x = 25.0;
double result = sqrt(x);
printf("The square root of %.2f is %.2f\n", x, result);
return 0;
}

Output:

Output

The square root of 25.00 is 5.00

2. pow - Exponential Power Function

The pow function raises a number to a given power.

Example

Example

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

int main() {
double base = 2.0;
double exponent = 3.0;
double result = pow(base, exponent);
printf("%.2f raised to the power of %.2f is %.2f\n", base, exponent, result);
return 0;
}

Output:

Output

2.00 raised to the power of 3.00 is 8.00

3. sin, cos, and tan - Trigonometric Function

These formulas calculate an angle's sine,cosine , and tangent in radians.

Example

Example

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

int main() {
double angle = 1.0; // in radians
double sin_val = sin(angle);
double cos_val = cos(angle);
double tan_val = tan(angle);

printf("For angle %.2f:\n", angle);
printf("Sine: %.2f\n", sin_val);
printf("Cosine: %.2f\n", cos_val);
printf("Tangent: %.2f\n", tan_val);

return 0;
}

Output:

Output

For angle 1.00:
Sine: 0.84
Cosine: 0.54
Tangent: 1.56

4. exp - Exponential Function

The exp function determines the number's exponential value (ex) .

Example

Example

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

int main() {
double x = 2.0;
double result = exp(x);
printf("e raised to the power of %.2f is %.2f\n", x, result);
return 0;
}

Output:

Output

e raised to the power of 2.00 is 7.39

5. log and log10 - Logarithmic Functions

The log function calculates the natural logarithm (base e) of a number, while log10 calculates the common logarithm (base 10) .

Example

Example

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

int main() {
double x = 100.0;
double natural_log = log(x);
double common_log = log10(x);

printf("Natural logarithm of %.2f is %.2f\n", x, natural_log);
printf("Common logarithm of %.2f is %.2f\n", x, common_log);
return 0;
}

Output:

Output

Natural logarithm of 100.00 is 4.61
Common logarithm of 100.00 is 2.00

6. ceil and floor - Rounding Functions

The ceil function rounds a number up to the nearest integer, while floor rounds it down.

Example

Example

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

int main() {
double x = 3.7;
double y = -2.3;

printf("Ceil of %.2f is %.2f\n", x, ceil(x));
printf("Floor of %.2f is %.2f\n", x, floor(x));

printf("Ceil of %.2f is %.2f\n", y, ceil(y));
printf("Floor of %.2f is %.2f\n", y, floor(y));

return 0;
}

Output:

Output

Ceil of 3.70 is 4.00
Floor of 3.70 is 3.00
Ceil of -2.30 is -2.00
Floor of -2.30 is -3.00

7. fabs - Absolute Value Function

The fabs function returns the absolute value of a given number, which is its distance from zero.

Example

Example

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

int main() {
double x = -5.0;
double result = fabs(x);
printf("The absolute value of %.2f is %.2f\n", x, result);
return 0;
}

Output:

Output

The absolute value of -5.00 is 5.00

8. fmod - Remainder Calculation Function

The remaining value of a two-number division is calculated using the fmod function .

Example

Example

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

int main() {
double dividend = 10.0;
double divisor = 3.0;
double remainder = fmod(dividend, divisor);
printf("The remainder of %.2f divided by %.2f is %.2f\n", dividend, divisor, remainder);
return 0;
}

Output:

Output

The remainder of 10.00 divided by 3.00 is 1.00

9. sinh and cosh - Hyperbolic Sine and Cosine Functions

A given integer hyperbolic sine or cosine can be calculated using the sinh and cosh functions.

Example

Example

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

int main() {
double x = 2.0;
double sinh_val = sinh(x);
double cosh_val = cosh(x);

printf("Hyperbolic sine of %.2f is %.2f\n", x, sinh_val);
printf("Hyperbolic cosine of %.2f is %.2f\n", x, cosh_val);

return 0;
}

Output:

Output

Hyperbolic sine of 2.00 is 3.63
Hyperbolic cosine of 2.00 is 3.76

10. atan2 - Arc Tangent Function (2 Arguments)

Considering both of the arguments' signs, the atan2 function calculates the arctangent of the quotient of its two inputs.

Example

Example

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

int main() {
double x = 1.0;
double y = 1.0;
double result = atan2(y, x);
printf("The arctangent of y=%.2f and x=%.2f is %.2f radians\n", y, x, result);
return 0;
}

Output:

Output

The arctangent of y=1.00 and x=1.00 is 0.79 radians

11. round - Rounding to Nearest Integer

When given a floating-point number, the round function returns the closest integer.

Example

Example

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

int main() {
double x = 3.6;
double y = 3.3;

printf("Rounded value of %.2f is %.2f\n", x, round(x));
printf("Rounded value of %.2f is %.2f\n", y, round(y));

return 0;
}

Output:

Output

Rounded value of 3.60 is 4.00
Rounded value of 3.30 is 3.00

Input Required

This code uses input(). Please provide values below: