Display the given series in Python.
2 3 2 3 2 3 . . . . . .N terms.
Don't use any conditional constructs or arrays or strings. Use numeric approach in solving.
Answers
Answered by
8
We have to write a Python program to display 2 3 2 3 .... following series till n terms.
Restrictions:-
- No conditional construct
- No arrays
- No strings
(since arrays and lists are different, here we can use lists)
Main concept:-
On dividing integers (considering only whole numbers here) by 2, the remainder is in the pattern of 0 1 0 1 ....
We observe that,
- 0 + 2 = 2
- 1 + 2 = 3
- 0 + 2 = 2
- 1 + 2 = 3
and so on.
Required program:-
print(*[n%2 + 2 for n in range (0, (int(input("Enter the number of terms: "))))])
Output:-
Enter the number of terms: 5
2 3 2 3 2
Algorithm:-
- Taking input from the user about the number of terms of the series to be printed.
- Using the for loop and the range function to iterate the loop the required number of times.
- Storing the values of n%2 + 2 as the for loop iterates, in a list.
- Unpacking the list using * and printing the required pattern.
Attachments:
Similar questions