0000 Series 12 | MyCareerwise

FOR FREE CONTENT


Description

The program is written here to calculate the value of the series 1/2 - 2/3 + 3/4 - 4/5 + 5/6 - ...... n. The number of terms is taken as input from the user. A for loop is used here to find the value of the series.

If the value of ‘n’ is 3 then the value of the series will be:

1/2-2/3+3/4=0.58333333333333 (approx..)

Algorithm

INPUT: Number of terms

OUTPUT: Value of the series

PROCESS:

Step 1: [Taking the input]

               Read n [number of terms]

Step 2: [Calculating the value of the series]

             Set s<-0

             Set sign<-1

              [Calculating the series]

               For i=1 to n repeat

                              Set s<-s+(sign×(i/(i+1)))

                              Set sign<-sign×(-1)

               [End of ‘for’ loop]

               [Printing the result]

               Print "The value of the series is: s"

Step 3: Stop.

Code

TIME COMPLEXITY:

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

               {

                              s=s+(sign*(i/(i+1)));

                              sign=sign*(-1);

               }

The time complexity of this program is O(n) where ‘n’ is the number of terms of the series

SPACE COMPLEXITY:

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