If you were asked to print the series given below using Python, what would be the shortest códe possible?
1 0 1 0 1 0...10 terms.
Answers
Answered by
18
Program
print(*[i % 2 for i in range(1, int(input("Enter N - ")) + 1)])
Algorithm
- Accepting the number of terms.
- Getting the remainder of each number in range when divided by 2 hence, generating the pattern.
- Unpacking the list, therefore printing the pattern on the screen.
Attachments:
anindyaadhikari13:
You are Genius. Awesome
Answered by
36
Programming language:-
- Python
k=1
for i in range(0, 10 + 1):
print(k%2,end=" ")
k += 1
Variable Description:-
1. k:- local variable to print the terms
2. i:- loop variable
Explaination:-
• The Program intialized k=1
• Then it runs i loop from 1 to 10
• Then k%2 where k is divided by 2 and remainder gets printed i.e. either 0 or 1
• k+=1 update the value of k
____
•Output Attached
Attachments:
Similar questions
Social Sciences,
1 month ago
History,
1 month ago
Science,
1 month ago
Hindi,
3 months ago
Math,
3 months ago
Geography,
9 months ago
Science,
9 months ago
Social Sciences,
9 months ago