Pages

Saturday

Program to Calculate the Cosine Series in C | C Program

Here's a C program to calculate the cosine 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 Cosine Series: The formula for cosine series is -

Cosine Series

# include <stdio.h>
# include <conio.h>
# include <math.h> 
void main() 
{ 
 int i, n ; 
 float x, val, sum = 1, t = 1 ; 
 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 ; 
 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("\nCosine value of %f is : %8.4f", val, sum) ; 
 getch() ; 
} 

Output of above program is

Enter the value for x : 60
Enter the value for n : 20
Cosine value of 60.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 1. So, after line no 16 of the program, we have:
t = 1 and sum =1
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 = 1, sum =1
Putting these values in Exp 1 we get,
t = 1 * pow((double) (-1), (double) (2 * 1 - 1)) * x * x / (2 * 1 * (2 * 1 - 1)) 
t = 1*(-1)*x*x/2 = -x2/2!
Putting value of t in Exp 2 we get,
 
sum =  1 + (-x2/2! ) = 1 - x2/2! 
Iteration 2: i = 2, t = -x2/2!, sum = 1 - x2/2!
Putting these values in Exp 1 we get,
t = (-x2/2!) * pow((double) (-1), (double) (2 * 2 - 1)) * x * x / (2 * 2 * (2 * 2 - 1)) 
t = -x2/2!*(-1)*x*x/12 = x4/4!
Putting value of t in Exp 2 we get,
 
sum = 1 - x2/2! + x4/4! 
Iteration 3: i = 3, t = x4/4!, sum = 1 - x2/2! + x4/4!
Putting these values in Exp 1 we get,
t = x4/4! * pow((double) (-1), (double) (2 * 3 - 1)) * x * x / (2 * 3 * (2 * 3 - 1)) 
t = x4/4!*(-1)*x*x/30 = x6/6!
Putting value of t in Exp 2 we get,
 
sum = 1 - x2/2! + x4/4! - x6/6!
After iteration 3, i = 4 so the for loop terminates and output is printed.

14 comments:

  1. To avoid precision/accuracy loss to some extent and also unexpected behavior caused while storing floating point values in memory.

    ReplyDelete
  2. how to do the same cosx nd sinx series using do-while loop??

    ReplyDelete
  3. Just initialize i with 1 outside the loop and replace the for loop as follows:

    i = 1;
    do{
    t = t * pow((double) (-1), (double) (2 * i - 1)) *
    x * x / (2 * i * (2 * i - 1)) ;
    sum = sum + t ;
    i++;
    }while( i < n + 1);

    Hope it works.

    ReplyDelete
  4. Without using pow and double, how to do the same program

    ReplyDelete
  5. You could replace the entire expression where pow and double are used as follows:

    t=t+return__1(i)

    Here return__1(i) would return -1 if i is odd and 1 if i is even.

    Float return__1(i){
    If(i%2==0){//even
    Return 1;
    }
    Else//odd
    Return -1;
    }

    ReplyDelete
  6. What is the purpose of writing %8.4f ? Explain

    ReplyDelete
  7. Hi,

    This is a way to format the output. It means print upto 8 positions before decimal. This is called width. Second, 4 means we want to print 4 digits after the decimal. This is precision.

    ReplyDelete
  8. Tell me the use of double and %8.4f ?

    ReplyDelete
    Replies
    1. Please refer to the comments above. In short, double is used in place of float as it has bigger size than float and the %8.4f is to format the display of the value.

      Delete
  9. This comment has been removed by the author.

    ReplyDelete
  10. #include stdio.h>//removed

    int main()
    {
    int i,n,z=-1,p=2;
    float x,s=1,r;

    printf("Enter value of x");
    scanf("%f", &x);

    printf("Enter value of n");
    scanf("%d", &n);

    x=x*(3.14/180);

    while(p<=n)
    {
    r=pow(x,p)/fact(p);
    s=s+(z*r);
    z=z*-1;
    p+=2;
    }
    printf("The sum of cosine series is %f\n", s);
    }

    int fact(int u)
    {
    int j,y=1;
    for(j=1;j<=u;j++)
    {
    y=y*j;
    }
    return y;
    }

    ReplyDelete
  11. Hallo,


    Fully agree on Formula of Cosine Series. We’re seeing a lot of projects tackle big complex problems but few seem to have taken into consideration and in particular reasons to adopt.

    Can you explain me why this code has two pointers?

    Code:
    char ** binaries; // Why two pointers here? Why pointer to pointer?
    binaries = (char **)malloc( sizeof(char *)*nDevices );
    Complete code for this can be found on Paste.of code U5g2Z3Cj5ZuRBnJf2b5cqv


    Very useful article, if I run into challenges along the way, I will share them here.


    Cheers,

    ReplyDelete
  12. How to do the same program using while loop without using pow

    ReplyDelete