# Thread: UDF to calculate sine

1. ## UDF to calculate sine

I was asked to calculate sine using UDF.. using pow is not allowed... i did the following way...
Code:
```#include <stdio.h>
#include <conio.h>
int fact(int num);
float powe(float base, int power);
float sine(float x);
int main(void)
{
float xx,x;
printf("Enter the angle in degree : ");
scanf("%f",&xx);
while(xx>=360)
xx=xx-360;
if (xx>=270) xx=xx-360;
if(xx>=180 && xx<270) xx= xx-180;
if(xx>90 && xx<180) xx=180-xx;
x=3.141592654/180*xx;
printf("Sin %d = %.4f",(int)xx,sine(x));
getch();
return 0;
}

float sine(float x)
{
int i;
float sine=0;
for(i=1;i<=10;i++)
sine=sine+powe(-1,i+1)*(powe(x,2*i-1)/fact(2*i-1));
return sine;
}

int fact(int num)
{
int i,p=1;
for (i=1;i<=num;i++)
{
p=p*i;
}
return p;
}

float powe(float base , int power)
{
int i;
float ans=1;
for(i=0;i<power;i++)
ans=ans*base;
return ans;
}```
if i dont use the codes in red, it cant calculate if the value of degree is higher... is there any other simpler way to create this function..?

2. sin x = x - x^3/3! + x^5/5! - x^7/7! + x^9/9!......................

that was the formula i used to calculate sine

3. Are you sure your series isn't supposed to take a radian value for the angle, rather than degrees?

--
Mats

4. the input is in degree but it has to be converted into radian for calculating the value of sine...

5. Also, as an addition to what matsp said, I don't think this code even works. It will calculate 29!, which is way too high for any usual integer in C.

6. i checked... it works .... but still i reduced the loop count to 10 now...