126k views
2 votes
) Printing odd numbers. Write Python code that asks the user for a positive number, then prints all the odd numbers from 0 to the number provided by the user. For example, if the user provides the number 7, then you should print the values: 1 3 5 7 *You can use 'range()' with three numbers.

User DDiamond
by
4.8k points

1 Answer

4 votes

Answer:

#here is code in Python

#read the value of n

n=int(input("enter a number:"))

#print all the odd numbers

for i in range (1,n+1,2):

print(i,end = '')

Step-by-step explanation:

Read a number from user and assign it to variable "n".Then use a for loop to print all the odd numbers from 1 to n.We can use range() function to print this. here first number is starting of the range and second will be the end of the range. and 3rd one is the Increment to the iterator.

For example In the above code, 1 is the start point and n+1 will be the last. Here

loop will run till n only.Every time value of "i" is increased by 2.

Output:

enter a number:15

1 3 5 7 9 11 13 15

User PixelEinstein
by
5.2k points