0000
Here the program is written to print the series 1 2 3 6 9 18 27 54.. For loop is used to print the series.
For the even terms the value will be: 2 × (previous term)
For the odd terms the value will be: (3/2) × (previous term)
The value of the first term is 1.
If the given number of terms is 4,
1st term: 1
2nd term (even term): 2×1=2
3rd term (odd term): (3/2) ×2 = 3
4th term (even term): 2×3=6
Therefore, the output will be: 1 2 3 6
INPUT: Number of terms
OUTPUT: The aforesaid series upto n terms
PROCESS:
Step 1: [Taking the input]
Read n [Number of terms]
Step 2: [Printing the series]
Set t<-1
[Printing the series]
Print "The series is: t "
For i=2 to n repeat
[If the term is even]
If i mod 2=0 then
Set t<-2×t
[If the term is odd]
Else
Set t<-(3×t)/2
[End of ‘if-else’]
[Printing the result]
Print t
[End of ‘for’ loop]
Step 3: Stop.
for(i=2;i<=n;i++)------------------------------------O(n)
{
//if the term is even
if(i%2==0)-------------------------------O(1)
t=2*t;
//if the term is odd
else---------------------------------------O(1)
t=(3*t)/2;
//printing the result
printf("%d ",t);
}
The time complexity of this program is O(n) where ‘n’ is the number of terms of the series.
The space complexity of this program is O(1) as it requires a constant number of memory spaces for any given input.