C - Math Function

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.

Note: degree = radian * (180 / pi)

C - abs() Function

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();
}
Enter number: -10
Absolute value = 10

fabs() function

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();	
}
Enter number: -10.5
Absolute value of decimal number is 10.5

ceil() function

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();
}
Enter number: 10.1
After ceil() value is 11

floor() function

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();
}
Enter number: 10.1
After floor() value is 10

fmod() function

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();
}
Result = 1.000000

modf() function

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();
}
A = 10.5
B = 10
C = 5

pow() function

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();
}
2 power 3 = 8

sqrt() function

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();
}
Square root of 9 = 3

sin() function

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();
}
sin 90 = 0.893997

Exercise: Math Function

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();
}
Enter value of a, b, and c: 10 12 16 Area of Triangle = 59.924953