How To Print Odd Numbers In Increasing Order?
I need to print a sequence of odd numbers in increasing order. I can solve it only in decreasing order. num = int(input(print('Type any integer: '))) count = 1 while count <= n
Solution 1:
You can try:
count = 1
while count <= num:
print(count)
count += 2
Explanation: Check if count is less or equal(in case num is odd too) to num. Then, print count before adding to count in increments of 2.
Solution 2:
In this case instead of using a while
loop I'd use a for
, looping through all your elements:
for i in range(num):
if i%2 !=0:
print(i)
Or you can use list comprehension
:
d = [i for i in range(num) if i%2!= 0]
#Print increasing valuesprint(d)
#Print decreasing valuesprint(d[::-1])
EDIT: as suggested another possible implementation is:
for i in range(1, num, 2):
print i
The same expression could be also used with list comprehension:
d = [i for i in range(1, num, 2)]
Post a Comment for "How To Print Odd Numbers In Increasing Order?"