Python code for the following series.
1 2 3 4 5 1 3 5 7 9 1 4 7 10 13 1 5 9 13 17....N terms.
Answers
Answered by
2
Answer:
def series(j):
i=1
sum=0
c=1
while i<6:
sum=sum+c
c=j
print(sum,end=" ")
i+=1
j=1
while j<5:
series(j)
j+=1
Explanation:
Answered by
2
Question:-
- Python Code to print this series
- 1 2 3 4 5 1 3 5 7 9 1 4 7 10 13 1 5 9 13 17....N terms.
It is a very interesting question....only a logic is required to understand this series.
In first 5 terms, the difference is 0(1,2,3,4,5)
In next five terms, difference is 1(1,3,5,7,9)
In next five terms, difference is 2(1,4,7,10)
And after each 5th term the number returns to 1 and the difference increases numerically
Since we found this logic, now code which I am going to write is based on this logic.
___________
Code:-
#code written by@swetank232894
n=(int)(input("Enter a number:"))
a,b,c=1,1,1
for i in range(1,n+1):
print(a,end=" ")
a+=b
c+=1
if(c>5):
c=1
b+=1
a=1
__________
Variable Description:-
- i:- loop variable
- a :- to calculate and print the terms
- b:- to add the difference to the variable a
- c:- counter variable
- n:- to accept Number of terms from the user
__________
•Output Attached
Attachments:
Similar questions