The program is written to find the value of the below series
The number of terms is taken as input from the user. The value of the series is calculated by using a ‘for’ loop.
For odd numbers the value of the term is added and for the even numbers the value of the terms is subtracted.
For example, if the number of terms be 2, the value of the series will be:
INPUT: Number of terms
OUTPUT: The 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.0
Set sign<-1
[Finding the sum]
For i=1 to n repeat
Set s<-s+sign×(i/ii)
Set sign<-sign×(-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+sign*(i/pow(i,i));
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.
Related