15,795,318 members
See more:
i want to find sin of x using sine series . x-x^3/3!+......
i want to find this using pow function

What I have tried:

C++
```#include<stdio.h>
#include<math.h>
int main()
{
int i,j,n,fact,ch,p;
float x,sum,angle;
do
{
printf("\n1. Sine series\n2. Cosine series\n3. Exponential series\n4. Exit\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the value of x(in degree)and n in sine series.\n");
scanf("%f",&x);
printf("e");
scanf("%d",&n);
printf("\n%d",n);
angle=x;
printf("hi");
x=angle*3.14/180;
fact=1;
sum=0;
p=0;
for(i=1;i<=n;i+2)
{
for(j=2;j<=n;j++)
{
fact=fact*j;
}
//printf("\n%d",fact);
sum+=(pow(-1,p)*((float)pow(x,i)/fact));
p++;
}
printf("\nsin(%.2f)=%.2f\n",angle,sum);
break;
case 2:
printf("\nEnter the value of x(in degree)and n in cosine series .\n");
scanf("%f%d",&x,&n);
angle=x;
x=x*3.14/180;
fact=1;
sum=0;
for(i=0,p=0;i<=n;i+2,p++)
{
for(j=1;j<=n;j++)
{
fact*=j;
}
sum+=(pow(-1,p)*((float)pow(x,i)/fact));
}
printf("\ncos(%.2f)=%.2f\n",angle,sum);
break;
case 3:
printf("\nEnter the value of x(in degree)and n in exponential series.\n");
scanf("%f%d",&x,&n);
angle=x;
x=x*3.14/180;
fact=1;
sum=0;
for(i=0;i<n;i++)
{
for(j=1;j<=n;j++)
{
fact*=j;
}
sum+=((float)pow(x,i)/fact);
}
printf("\ne^(%.2f)=%.2f\n",angle,sum);
break;
case 4:
exit(0);break;
default:printf("\n Invalid input\n");
}
}while(1);
return 0;
}```
Posted
Updated 10-Mar-17 12:02pm
v2
Rick York 10-Mar-17 12:07pm
All right then. What is your question?

I am guessing it is how do I format this correctly.

## Solution 1

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Time for you to learn a new (and very, very useful) skill: debugging!

Try it yourself, you may find it is not as difficult as you think.

## Solution 2

Quote:
i want to find sin of x using sine series . x-x^3/3!+......
i want to find this using pow function

Then do it, start working.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

## Solution 3

There are several errors. For instance
Quote:
for(i=1;i<=n;i+2)

is wrong, the last statement in the for clause ha no effect, it should be `i+=2`. Moreover, the `fact` variable must be initialized immediately before the factorial loop (by the way, it is better using a `float` variable for holding the factorial result, because it becomes large soon).
You might fix your code this way:
C
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j,n,ch,p;
float fact;
float x,sum,angle;
do
{
printf("\n1. Sine series\n2. Cosine series\n3. Exponential series\n4. Exit\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the value of x(in degree)and n in sine series.\n");
scanf("%f",&x);
scanf("%d",&n);
angle = x;
x=x*M_PI/180;
sum=0.0;
p=0;
for(i=1;i<=n;i+=2)
{
fact=1;
for(j=2;j<=i;j++)
{
fact=fact*j;
}
sum+=(pow(-1,p)*((float)pow(x,i)/fact));
p++;
}
printf("\nsin(%.2f)=%.2f\n",angle,sum);
break;
//...```

However I would factor out the `sin` computation in a function, and I would also compute the terms incrementally, e.g.
C
```#include <stdio.h>
#include <math.h>

double comp_sin( double x, int n)
{
double sign = 1.0;
double fact = 1.0;
double sum=0.0;
double x2 = x * x;

int k;

for (k=0; k<n; ++k)
{
sum += (sign * x) / fact;
// update the factors
sign = sign > 0.0 ? -1.0 : 1.0; // toggle the sign
fact *= (2.0 * k + 2.0) * (2.0 * k + 3.0); // compute the next 'factorial' factor
x *= x2; // compute the next 'power' factor
}
return sum;
}

int main()
{
int n;
double angle;

printf("please enter the angle in degres and the number of terms of the series\n");
scanf("%lf", &angle);
scanf("%d", &n);

printf("sin(%g) = %g\n", angle, comp_sin(angle*M_PI/180, n ));

return 0;
}```