The program is written here to find the value of the below series
Here, the number of terms is taken as input.
The value of the series is calculated by using a ‘for’ loop.
For example, if the input is 4, the output will be:
INPUT: Number of terms
OUTPUT: The value after calculating the sum
PROCESS:
Step 1: [Taking the input]
Read n [number of terms]
Step 2: [Finding the sum of the series]
Set sum<-0
Set a<- -3
Set t<-4
[Finding the sum]
For i=1 to n repeat
Set sum<-sum+t
Set a<-a×(-1)
Set t<-t+a
[End of ‘for’ loop]
[Printing the sum]
Print "The sum of the series is: sum"
Step 3: Stop
TIME COMPLEXITY:
for(i=1;i<=n;i++)---------------------------------- O(n)
{
sum=sum+t;
a=a*(-1);
t=t+a;
}
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 the program is O(1) as it requires a constant number of memory spaces for any given input.
Related