FOR FREE YEAR SOLVED

Cos Series

Back to Programming

Description

Cos series in C, C++,Java and Python

The program is written here to calculate the value of the cosine series. The series is used to calculate the value of cos(x). Here, ‘x’ is the value of the angle which is taken as the input, ‘n’ is the number of terms which is also taken as input.

 

The cosine series is:

 

 

# The value of x is given in degree. Then it is converted into its equivalent radian. The value of the sine series is then calculated.

 

# If the value of ‘x’ is 90 then the value of cos(x) = 0.

Algorithm

INPUT: The value of the angle and the number of terms.

OUTPUT: The value of cos(x)

PROCESS:

Step 1: [Taking the input]

               Read n [number of terms]

               Read x [the angle for which the value is to be calculated]

Step 2: [Finding the value of sin(x)]

               Set s<-1

               Set sum<-0

               [Finding the sum of the series]

               For i=0 to n incremented by 2 repeat

                              Set f<-1

                              [Finding the factorial]

                              For j=1 to i repeat

                                             Set f<-f×j

                              [Finding the sum of the series]

                              Set sum<-sum+s × xi/f

                              Set s<-s×(-1)

               [End of ‘for’ loop]

               Print "The value of cos(x) is sum"

Step 3: Stop.

Code

TIME COMPLEXITY:

for(i=0;i<=n;i=i+2)----------------------------------------------- O(n)

               {

                              f=1;

                              //finding the factorial

                              for(j=1;j<=i;j++)---------------------------------------- O(i)

                                             f=f*j;

                              //finding the sum of the series

                              sum=sum+s*(pow(x,i)/f);

                              s=s*-1;

               }

 

 

The time complexity of this program is O(n*i) where ‘n’ is the number of terms, ‘i’ is the power of each term.

 

SPACE COMPLEXITY:

The space complexity of this program is O(1) as it requires a constant number of spaces to execute the program for any given input.

Contributed by