C Mathematical Operations (Math.h)

C provides various mathematical operations support through the math.h header file. Some of the common math functions defined in this header file are pow(), ceil(), floor(), etc.

The following code shows the output of various mathematical operations using <math.h>.

Code

#include<stdio.h>
#include<math.h>
#define PI 3.141592

int main(void)
{
	
	double x = 10.2;
	
	double y1 = pow(x,2);	// pow(number,raiseto)
	printf("%lf^2 = %lf\n",x,y1);
	
	int y2 = ceil(x);	// ceiling function (smallest integer greater than x)
	printf("ceil(%lf) = %d\n",x,y2);
	
	int y3 = floor(x);	// floor function (largest integer less than x)
	printf("floor(%lf) = %d\n",x,y3);
	
	int y4 = abs(-10);	// abs function (removes sign)
	printf("abs(-10) = %d\n",y4);
	
	double y5 = sqrt(2);	// square root function (can be replaced by pow(2,0.5)
	printf("sqrt(2) = %lf\n",y5);
	
	double y6 = log10(100);	// log base 10
	printf("log10(100) = %lf\n",y6);
	
	double y7 = log(100);	// log base 10
	printf("log(100) = %lf\n",y7); // log base e
	
	double y8 = exp(3);	// e^x
	printf("e^3 = %lf\n",y8);
	
	double y9 = sin((PI/180)*90);	// sin(x) returns sin of radian x
	printf("sin(90) = %lf\n",y9); // 90 in degree
	
	// similarly you can try for tan(x), cos(x), sinh(x), cosh(x), tanh(x), 
	// asin(x), acos(x), atan(x), atan2(x) 
	
	return 0;
}

Output

10.200000^2 = 104.040000
ceil(10.200000) = 11
floor(10.200000) = 10
abs(-10) = 10
sqrt(2) = 1.414214
log10(100) = 2.000000
log(100) = 4.605170
e^3 = 20.085537
sin(90) = 1.000000

Trending

C Programming

Remember to follow community guidelines and don't go off topic.