Sunday, 5 March 2017

Mathematical functions in C/C++ Program in Govindpuram

Mathematical functions
Mathematical functions are defined in the math.h header file. These functions are used for arithmetic caluculations of the variables and constants as defined. The important functions of math.h are fabs(), pow(), sqrt(), sin(), cos() and abs().The use of these functions are given below:

fabs()-returns the absolute value of num
pow()-returns base raised to exp power i.e., base exp. A domain error occurs if base = 0 and exp <= 0. Also if base < 0 and exp is not integer.
sqrt()-returns the square root of num. If num < 0, domain error occurs
sin()-returns the sin of arg. The value of arg must be in radians
cos()-returns the cosine of arg. The value of arg must be in radians
abs()-returns the absolute value of num

To use understand the use of above functions, let consider the given example:
Example menu-driven program to show the use of string functions

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
         clrscr();

         short int si = 100;
         int i = -1000;
         long int li = 1300;
         float f = 230.47;
         double d = 200.347;
         double x = -2.100000;

         cout<<"sqrt(si): "<<sqrt(si)<<endl;
         cout<<"pow(li, 3): "<<pow(li, 3)<<endl;
         cout<<"sin(d): "<<sin(d)<<endl;
         cout<<"abs(i) : "<<abs(i)<<endl;
         cout<<"floor(d): "<<floor(d)<<endl;
         cout<<"sqrt(f): "<<sqrt(f)<<endl;
         cout<<"pow(d, 2): "<<pow(d, 2)<<endl;
         cout<<"fabs(d): "<<fabs(d)<<endl;
        
}
Output of Example program 

sqrt(si):10
pow(li, 3): 2.197e+09
sin(d): -0.655564
abs(i) : 1000
floor(d): 200
sqrt(f): 15.181238
pow(d, 2): 40138.920409

fabs(d): 2.100000

No comments:

Post a Comment