# C Program For Cosine Series

Hi there! Welcome to the newest post to brush your coding skills which is c program for Cosine Series.

This post will implement the sum of series of a cosine function to calculate the cosine value in a C Program. This code will help you to implement this algorithm in other languages as well.

Let’s get started!

## Cosine Function:

In mathematics, the different trigonometric functions are sine, cosine, tangent, cotangent, secant and cosecant.

Each of these functions follow a general series which determine their values if associated with an integer. These come in handy in various other problems as well. Consider them as helping hands in approximations of many values in problems.

They formulate equations helpful for approximations and assumptions of values related to space, architecture, science, etc.

Among these, the cosine function or cosx (x is any arbitrary integer) is a periodic function with a period of 2π. In a right-angled triangle, with an angle x, a cosine function is represented as:

cosx = b/h

Where b is the base of the triangle and h is its hypotenuse.

The graph of a cosine function for your reference: As you can see in the graph, it repeats itself after a period of 2π.

Some of the properties of cosine function:

1. The cosine function is even. Hence, it is symmetrical about y-axis
2. It is a continuous function.
3. It follows the rule cosx= cos(2π+x). Therefore, it is periodic about 2π
4. The range of a cosine function is [-1,1]. I.e. -1cosx.
5. Curve of the graph of this function cuts x-axis at 0,2,32,52,….

Cosine function is very useful in the field of mathematics as well as computer science. We require cosine values for solving complicated problems. Its repetitive nature helps us determine complex approximations and study the nature of certain functions.

## Cosine Series:

We can consider the cosine series as the sibling series of sine, tangent, exponential and many other important expansion series. These list of expansions are very useful when it comes to research analysis and solving equations where there is no accurate solutions.

The expansion series derive their origin from other complex series and have been modified through years of research. You can study about all these series to increase your knowledge in this field. It will also help you in your other coding problems.

The cosine series is represented by: This series is just like any other mathematical expansion series originated from the Taylor series. It is used for solving problems involving Fourier series.

In computer science, it is mainly used in the field of gaming in moving objects from one position to another.

Now, there is math.h function that will provide you accuracy upto 15 digits which is more than enough to use. But, learning to find the values from the series will add up to your backups in case standard libraries cannot be retrieved.  In addition, it will just be a fun little journey to figure out complex operations which will contribute to your programming adventures.

## Algorithm:

Solving a cosine series can be complex and difficult. Implementing it in a C program is more difficult. But we all know what makes it easier, algorithms. The algorithm below will walk you through the outline of what we are going to do in the program. Following this algorithm closely will clarify any complications or doubts that you might have while coding.

I suggest you go through the algorithm and try the coding for yourself before getting into the coding section. This way, you will improve your coding and thinking skills. Remember, when it comes to coding, its better to visualise and solve the problem beforehand rather than going for the solution right away.

So without further ado, let’s look into the algorithm.

1. Start
2. Initialise x and n from the user.
4. Convert x in the form of radians
5. Initialise sum ← 1 and t ←1
6. Make a loop for calculating the cosine value
7. Add the value to sum
8. Display sum
9. Stop

Hope that through this algorithm, you were able to understand the basic procedure that we are going to code.

## Program:

``//C Program to find the value of cosine using cosine series#include<stdio.h>#include<conio.h>void main(){    int i, n;     float x, sum=1, t=1;      printf(" Enter the value for x : ");     scanf("%f",&x);     printf(" Enter the value for n : ");     scanf("%d",&n);     x=x*3.14159/180;// Loop to calculate the value of Cosine    for(i=1;i<=n;i++)    {        t=t*(-1)*x*x/(2*i*(2*i-1));        sum=sum+t;  }    printf(" The value of Cos(%f) is : %.4f", x, sum);    getch();}``

## Output:

The following execution of the program takes user input of x as 1 and n as 4. ## Program Explanation:

The above code of c program for cosine series executes the evaluation of cosine value using cosine series. It follows the following steps:

Note: We are taking the example of n=4 and x=30.

1. Converting ‘x’ to radian value : x = x * 3.14159 / 180    (x = 30 * 3.14159 / 180)
Now,  x=0.52359
1. We assign t=1 and sum=1
2. 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 = (1 * (-1) * 0.52359 * 0.52359)/(2 * 1 * (2 * 1 – 1))
Now,  t = – 0.13707

sum = 1 + (- 0.1370) =0.86293
i++ i.e.  i=2

3.2.   i<=n    (2<=4)    for loop condition is true
t = (- 0.13707 * (-1) * 0.52359 * 0.52359)/(2 * 2 * (2 * 2 – 1))
Now,  t = 0.00313
sum = 0.86293 + 0.00313 = 0.86606
I++ i.e.  i=3

3.3.   i<=n    (3<=4)    for loop condition is true
t = (0.00313 * (-1) * 0.52359 * 0.52359)/(2 * 3 * (2 * 3 – 1))
Now,  t = – 0.000028
sum = 0.86606 + (- 0.000028) = 0.86603
i++ i.e.  i=4

3.4.   i<=n    (4<=4)    for loop condition is true
t = (-0.000028 * (-1) * 0.52359 * 0.52359)/(2 * 4 * (2 * 4 – 1))
Now,  t = 0.000000137
sum = 0.86603 + 0.000000137 = 0.866030137
i++ i.e.  i=5

3.5.  i<=n    (5<=4)    for loop condition is false. Iteration stops.

1. Finally it prints The value of  Cos(0.52359) is : 0.8660
2. Thus program execution is completed.

## Ending note :

In conclusion, it can be said that finding the value of cosine by the sum of series is helpful in many ways.

For example, you may need to implement it in some of the complex algorithms which involve other complex procedures. Not only that but cosine series, especially the sum of cosine series is a great tool in solving mathematical problems of school level or college level.

Learning to code this program of c program for cosine series will provide you your own cosine series calculator. It will ease your difficulty in solving problems as well as save you a lot of time.

The program is easy to understand and there are detailed explanations to help you out.  Feel free to ask doubts and share your views below.