Use of math functions
The standard mathematical functions defined and kept as a part of C math library.We must add an instruction as follows:
#include<math.h>
Math functions
function Meaning
Trigonometric
cos(x) Cosine of x
sin(x) Sine of x
tan(x) Tangent of x
acos(x) Arc cosine of x
asin(x) Arc sine of x
atan(x) Arc tangent of x
atan2(x,y) Arc tangent of x/y
Hyperbolic
cosh(x) Hyperbolic cosine of x
sinh(x) Hyperbolic sine of x
tanh(x) Hyperbolic tangent of x
Other functions
ceil(x) x rounded up to the nearest
integer
exp(x) e to the x power
fabs(x) Absolute value of x
floor(x) x rounded down to the
nearest integer
fmod(c) Remainder of x/y
log(x) Natural log of x,x>0
log10(x) Base 10 log of x,x>0
pow(x,y) x to the power y
sqrt(x) Square root of x,x>=0
sqrt(x) Square root of x,x>=0
1.A C program which accepts an angle and gives sine and cosine value.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int degree;
float angle;
float x,y;
clrscr();
printf("Enter angle in degree:");
scanf("%d",°ree);
angle=(degree*3.1416)/180;
x=sin(angle);
y=cos(angle);
printf("sin(%d°)=%f",degree
,x);
,x);
printf("\ncos(%d°)=%f",degree
,y);
,y);
getch();
}
Output:
Enter angle in degree:30
sin(30°) = 0.500001
cos(30°) = 0.866025
2.A C program to find out the roots of the quadratic equation.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,discriminant,root1,root2;
printf("Enter values of a,b and c of the quadratic equation:\n");
scanf("%f%f%f",&a,&b,&c);
discriminant=b*b-4*a*c;
if(discriminant<0)
{
printf("Roots are imaginary\n");
}
else
{
root1=(-b+sqrt(discriminant))/(2*a);
root2=(-b-sqrt(discriminant))/(2*a);
printf("Roots are:\n%f\n%f",root1,root2);
}
getch();
}
Output:
Enter values of a,b and c of the quadratic equation:
2 6 1
Roots are:
-0.177124
-2.822876
Another output:
Enter values of a,b and c of the quadratic equation:
4 2 1
Roots are imaginary
Interesting
ReplyDelete