Here's a C program to calculate the sine series with output. This program makes use of C concepts like For loop. The program also uses C's math.h header file and power function pow(i, j).
Formula of Sine Series: The formula for sine series is -
Sine Series |
# include <stdio.h> # include <conio.h> # include <math.h> void main() { int i, n ; float x, val, sum, t ; clrscr() ; printf("Enter the value for x : ") ; scanf("%f", &x) ; printf("\nEnter the value for n : ") ; scanf("%d", &n) ; val = x ; x = x * 3.14159 / 180 ; t = x ; sum = x ; for(i = 1 ; i < n + 1 ; i++) { t = (t * pow((double) (-1), (double) (2 * i - 1)) * x * x) / (2 * i * (2 * i + 1)) ; sum = sum + t ; } printf("\nSine value of %f is : %8.4f", val, sum) ; getch() ; }
Output of above program is
Enter the value for x : 30
Enter the value for n : 20
Sine value of 30.000000 is : 0.5000
Explanation of above program
Let n = 3, then the sine series generated by above program is as follows:
Initially, x is converted to radian by multiplying it by 3.14159/180 and t and sum are also assigned the value of x. So, after line no 16 of the program, we have:
t = x and sum = x
Now lets take a look at the working of for loop with n = 3:
Exp 1: t = (t * pow((double) (-1), (double) (2 * i - 1)) * x * x) / (2 * i * (2 * i + 1))
Exp 2: sum = sum + t
Iteration 1: i = 1, t = x, sum = x
Putting these values in Exp 1 we get,
t = (x * pow((double) (-1), (double) (2 * 1 - 1)) * x * x) / (2 * 1 * (2 * 1 + 1))
t = x*(-1)*x*x/6 = -x3/3!
Putting value of t in Exp 2 we get,
sum = x + (-x3/3! ) = x - x3/3!
Iteration 2: i = 2, t = -x3/3!, sum = x - x3/3!
Putting these values in Exp 1 we get,
t = (-x3/3! * pow((double) (-1), (double) (2 * 2 - 1)) * x * x) / (2 * 2 * (2 * 2 + 1))
t = (-x3/3!)*(-1)*x*x/20 = x5/5!
Putting value of t in Exp 2 we get,
sum = x - x3/3! + x5/5!
Iteration 3: i = 3, t = x5/5!, sum = x - x3/3! + x5/5!
Putting these values in Exp 1 we get,
t = (x5/5! * pow((double) (-1), (double) (2 * 3 - 1)) * x * x) / (2 * 3 * (2 * 3 + 1))
t = (x5/5!)*(-1)*x*x/42 = -x7/7!
Putting value of t in Exp 2 we get,
sum = x - x3/3! + x5/5! - x7/7!
After iteration 3, i = 4 so the for loop terminates and output is printed.
what is pow
@Chaitanya Reddy: pow is power function in C. For more information please refer this post - http://cprogramming.language-tutorial.com/2012/01/c-program-to-find-power-of-number-using.html
Why use double
in the brackets?
To avoid precision loss. Since pow((-1), (2 * i - 1)) i.e. without casting to double will result in an integer value. But in the above program, you are multiplying this value to a floating point value i.e. variable "t" and when float/double is multiplied by int type, precision loss occurs.
That is why pow((double) (-1), (double) (2 * i - 1)) equation is used which will result output in double. Double in brackets means that you are casting the value to double.
explain briefly.... cnt understad.. process of it.
@Teja: Explanation has been added in the post.
can we write t=(pow(-1,i)*pow(x,2i-1))/2 * i * (2 * i + 1)) ; insted of t = (t * pow((double) (-1), (double) (2 * i - 1)) *
x * x) / (2 * i * (2 * i + 1)) ; with out initializing t=x
why cant we use -1.0 instead of pow((double) (-1), (double) (2 * i - 1))
THANKS MAN YOU WERE SO HELPFUL!!!
Plz give algorithm for this program
We can use (-1) instead of pow((double)(-1),(double)(2*i-1)). I have tired it and it works. You can checkout the program in the link below: http://www.codingconnect.net/c-program-for-sine-series/
What is the use of n
Hi There,
I’ve often thought about this Program to Calculate the Sine Series in C | C Program. Nice to have it laid out so clearly. Great eye opener.
How can milli second / micro second / nano second be used in gcc platform? What is the process, syntax?
It was cool to see your article pop up in my google search for the process yesterday. Great Guide.
Keep up the good work!
Ciao,
Preethi
Pow means 'power' of x