C program for Sine Series
Before going to the program for Sine Series first let us understand what is a Sine Series?
Sine Series:
Sine Series is a series which is used to find the value of Sin(x).
where, x is the angle in degree which is converted to Radian.
The formula used to express the Sin(x) as Sine Series is
Expanding the above notation, the formula of Sine Series is
For example,
Let the value of x be 30.
So, Radian value for 30 degree is 0.52359.
So, the value of Sin(30) is 0.5.
Program code for Sine Series in C:
#include<stdio.h> #include<conio.h> void main() { int i, n; float x, sum, t; clrscr(); printf(" Enter the value for x : "); scanf("%f",&x); printf(" Enter the value for n : "); scanf("%d",&n); x=x*3.14159/180; t=x; sum=x; /* Loop to calculate the value of Sine */ for(i=1;i<=n;i++) { t=(t*(-1)*x*x)/(2*i*(2*i+1)); sum=sum+t; } printf(" The value of Sin(%f) = %.4f",x,sum); getch(); }
Related: C program for Cosine Series
Working:
- First the computer reads the value of ‘x’ and ‘n’ from the user.
- Then ‘x’ is converted to radian value.
- Then using for loop the value of Sin(x) is calculate.
- Finally the value of Sin(x) is printed.
Related: C program for Exponential Series
Step by Step working of the above Program Code:
Let us assume that the user enters the value of ‘x’ as 45 and ‘n’ as 4.
- Converting ‘x’ to radian value
x = x * 3.14159 / 180 (x = 45 * 3.14159 / 180) So, x=0.785398
- It assigns t=x and sum=x (i.e. t=0.785398 and sum=0.785398)
- It assigns the value of i=1 and the loop continues till the condition of the for loop is true.
3.1. i<=n (1<=4) for loop condition is true
t = (0.785398 * (-1) * 0.785398 * 0.785398)/(2 * 1 * (2 * 1 + 1))
So, t = – 0.08074
sum = 0.785398 + (- 0.08074)
So, sum=0.70465
i++
So, i=2
3.2. i<=n (2<=4) for loop condition is true
t = (- 0.08074 * (-1) * 0.785398 * 0.785398)/(2 * 2 * (2 * 2 + 1))
So, t = 0.00249
sum = 0.70465 + 0.00249
So, sum=0.70714
i++
So, i=3
3.3. i<=n (3<=4) for loop condition is true
t = (0.00249 * (-1) * 0.785398 * 0.785398)/(2 * 3 * (2 * 3 + 1))
So, t = – 0.000032
sum = 0.70714 + (- 0.000032)
So, sum=0.707108
i++
So, i=4
3.4. i<=n (4<=4) for loop condition is true
t = (- 0.000032 * (-1) * 0.785398 * 0.785398)/(2 * 4 * (2 * 4 + 1))
So, t = 0.000000274
sum = 0.707108 + 0.000000274
So, sum=0.707108274
i++
So, i=5
3.5. i<=n (5<=4) for loop condition is false
It comes out of the for loop.
- Finally it prints The value of Sin(0.785398) = 0.7071
- Thus program execution is completed.
Output:
TO DOWNLOAD THE PROGRAM CODE : CLICK HERE
why didn’t we use -1.0 instead of pow((double)(-1),(double)(2*i-1))
Hi Sahil, We can use -1.0 instead of pow((double)(-1),(double)(2*i-1)). I have tired it and it works.
Also thanks for asking such a good question..
Bro how to replace -1.0 pls provide a brief code.
Hi Aman, I have already updated the program and the code given above is the actual code.
can you put the C++ program for the same question?
it would really help me if you do.
Hi Rex,
Based on your request, I have put the post: C++ program for sine series.
hope it would help you.. 🙂
Very good
Thank you kuldeep.. 🙂
Very consistent, clear and basic explaination so that everyone can understand without much effort.
Amazing explanation!
amazing explanation
Amazing
Is very good
Good answer ji