The program is written here to find the output of the below series
The number of terms is taken as input from the user. One for loop is used here to find the sum of the series.
For example, if the number of terms is 4 then the output will be calculated as:
INPUT: The number of terms
OUTPUT: The sum of the series
PROCESS:
Step 1: [Taking the input]
Read n [number of terms]
Step 2: [Calculating the sum of the series]
Set s <- 0
Set t<-1
Set sg<-1
[Finding the sum]
For i = 1 to n repeat
Set s <- s + sg×t
Set t<-t+2
Set sg<-sg×(-1)
[End of ‘for’ loop]
[Printing the result]
Print "The sum of the series is: s"
Step 3: Stop.
TIME COMPLEXITY:
for (i = 1; i <= n; i++)----------------------------------- O(n)
{
s = s + sg*t;
t+=2;
sg=sg*(-1);
}
The time complexity of this program is O(n) where ‘n’ is the number of the series.
SPACE COMPLEXITY:
The space complexity of this program is O(1) as it requires a constant number of spaces for any given input.
Related