I have a task to make an output program like this
if input : 5
then output : 2,6,10,14,18
Output must as many of total number as the input
My previous code like this
n = 5
num = 2
i = 1
while i <= n:
if num % 2 == 0:
if num % 4 != 0:
print(num)
i = i 1
num = num 1
But my output was just 2 numbers, i should get 5 numbers.
2,6
Can anyone help me?
CodePudding user response:
Put the i 1 under the second if condition
if num % 4 != 0:
print(num)
i = i 1
CodePudding user response:
As you can see your output is following the Arithmetic progression
#### first number
a = 2
#### diffrence between the seq numbers
d = 4
#### number of terms in sequence
n = 5
### running loop n times
### a (n-1) * d ( geeting the nth didgit in AP `enter code here`)
for i in range(1, n 1):
print(2 (i-1)*d , end=',')