A Learning Platform

SUCCESS

Success = Discipline + Consistency + Sacrifice.
Discipline - in what you do.
Consistency - in what you do.
Sacrifice - for what you do.

                   -- Manjunatha C. P. --

math.h: C Math Library Functions and example program

math.h header file consists of various library functions related to mathematics. All the functions in math.h library take double as an argument and return double as the result.

To use any library function of math.h, include the header file #include<math.h> in your program.

<math.h> Library Functions

No. Function Description

1 ceil(x) Returns the smallest integer value greater than or equal to x
2 floor(x) Returns the largest integer value less than or equal to x.
3 sqrt(x) Returns the square root of x.
4 pow(base, exponent) Returns the power of given number.
5 abs(x) Returns the absolute value of x.
6 cos(radian_angle) Returns the cosine of a radian angle.
7 sin(radian_angle) Returns the sine of a radian angle.
8 exp(x) Returns the value of e raised to the xth power.
9 log(x) Returns the natural logarithm (base-e logarithm) of x.

Algorithm: Program to demonstrate math.h library functions

STEP 1: START
STEP 2: CALL ceil function
              ceil(number)
              PRINT “return value of ceil function”
STEP 3: CALL floor function
              floor(number)
              PRINT “return value of floor function”
STEP 4: CALL square root function
              sqrt(number)
              PRINT “return value of sqrt function”
STEP 5: CALL power function
              pow(base & exponent)
              PRINT “return value of pow function”
STEP 6: CALL absolute function
              abs(number)
              PRINT “return value of abs function”
STEP 7: END

Example C program: math.h library funcions

#include<stdio.h>  
#include <math.h> 
#include <conio.h>   
int main(){ 
    printf("Demonstration of math.h library functions:");   
    printf("\nceil(5.3): %f",ceil(5.3));    
    printf("\nceil(5.7): %f",ceil(5.7));    
    printf("\nfloor(1.6): %f",floor(1.6));    
    printf("\nfloor(1.4): %f",floor(1.4));    
    printf("\nsqrt(12)): %f",sqrt(12));    
    printf("\npow(3,2): %f",pow(3,2));    
    printf("\nabs(-7): %d",abs(-7));
    printf("\nexp(3): %f",exp(3));
    printf("\nlog(2): %f",log(2));
    getch();     
    return 0;    
}    

Output:

Demonstration of math.h library functions:
ceil(5.3): 6.000000
ceil(5.7): 6.000000
floor(1.6): 1.000000
floor(1.4): 1.000000
sqrt(12)): 3.464102
pow(3,2): 9.000000
abs(-7): 7
exp(3): 20.085537
log(2): 0.693147

QUIZ:

1] C mathematical functions are present in header file.
  1. mathematics.h
  2. math.h
  3. geometry.h
  4. ctype.h
2] Which function returns the smallest integer value greater than or equal to x.
  1. ceil()
  2. floor()
  3. small()
  4. great()
3] Which of the following is not in math.h header file.
  1. pow()
  2. cos()
  3. log()
  4. strlen()
4] math.h is a
  1. function
  2. header file
  3. macro
  4. global variable