C Language defines various mathematical functions that can be used for calculation in our program. These functions are stored in math.h, stdlib.h header file.
S.No. | Functions | Syntax | Description |
---|---|---|---|
abs() | abs(value); | Returns the absolute value of a integer number i.e. convert negative value into positive value. | |
fabs() | fabs(float value); | Returns the absolute value of a float number. | |
ceil() | ceil(decimal value); | Remove the decimal places and return the integer number. It returns one value greater to the original number. | |
floor() | floor(decimal value); | Remove the decimal places and return the integer number. It returns one value smaller to the original number. | |
fmod() | fmod (x, y); | Divide two float numbers and return the reminder. | |
modf() | double a, b; double c=modf(a,b); | Splits the double number into number and its decimal value. Note: a,b,c should be in double. |
|
pow() | c = pow (a, b); | Calculate a power b. Note: a and b can be int, long, float, double. |
|
sqrt() | s=sqrt(n); | Calculate the square root of a number. Number can be int, long, float, double. |
|
sin() | sin(value); | Calculate the sin value in radians. Value should be in decimal. |
|
cos() | cos(value); | Calculate the cos value in radians. Value should be in decimal. |
|
tan() | tan(value); | Calculate the tan value in radians. Value should be in decimal. |
Note: degree = radian * (180 / pi)
The abs() function return the absolute value of the given integers. The abs() function only returns the positive interger number and passed any data type number either negative or positive.
Example 1: Math abs() Function
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int a; clrscr(); printf("Enter number: "); scanf("%d",&a); printf("Absolute value = %d", abs(a)); getch(); }
The fabs() function takes a single argument (in double) and returns the absolute value of that number (also in double).
To find absolute value of an integer or a float number, you can explicitly convert the number to double.
Example 2: Math fabs() function.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { double a; clrscr(); printf("Enter number: "); scanf("%lf",&a); printf("Absolute value of double data type is = %lf", fabs(a)); getch(); }
Example 3: Math ceil() function.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { double a; clrscr(); printf("Enter number: "); scanf("%lf",&a); printf("After ceil() value is %lf", ceil(a)); getch(); }
Example 4: Math floor() function.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { double a; clrscr(); printf("Enter number: "); scanf("%lf",&a); printf("After floor() value is %lf", floor(a)); getch(); }
Example 5: Math fmod() function.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { float a=11.5, b; clrscr(); b=fmod(a,3.5); printf("Result = %f\n", b); getch(); }
Example 6: Math modf() function.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { double a=10.5, b,c; clrscr(); c=modf(a,&b); printf("A = %lf", a); printf("B = %lf", b); printf("C = %lf", c); getch(); }
Example 7: Math pow() function.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int a=2, b=3, c; clrscr(); c=pow(a,b); printf("%d power %d = %d",a,b,c); getch(); }
Example 8: Math sqrt() function.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int b=9; double d; clrscr(); d=sqrt(b); printf("Square root of %d = %lf",b,d); getch(); }
Example 9: Math sin() function.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { double d; clrscr(); d=sin(90); printf("sin 90 = %lf",d); getch(); }
Exercise 1: Program to calculate Area of Triangle by using Math Library Function - sqrt().
#include<stdio.h> #include<conio.h> #include<math.h> void main() { float s,a,b,c,ar; clrscr(); printf("Enter value of a, b, and c: "); scanf("%f%f%f",&a, &b, &c); s=(a+b+c)/2; ar=sqrt(s*(s-a)*(s-b)*(s-c)); printf("Area of Triangle = %f", ar); getch(); }