Question

Which of the following code will print all odd numbers between 3 and 11, each number separated by a space as shown below 35 7

python programming

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question:
Which of the following code will print odd numbers between 3 and 11, each number separated by a space as shown below:
3 5 7 9 11
Ans:
for i in range(3,12,2):
print(i,end=" ")


Now check with the options, what each loop prints:
1. In the first loop
for i in range(3,11,1):
if i%2 != 0:
print(i,end=" ")
This will print the values: 3 5 7 9 but not 11, because 11 is not included, only up to 10, is included. so we are incrementing it by 1 and the if condition checks if the number is not divisible by 2. If we place 12 in place of 11, then this loop will print the exact same output needed.

2. In the second loop
for i in range(3,10,2):
print(i,end=" ")
This will print the values 3 5 7 9. Here, we are starting from 3, and ending up to 10(10 is excluded) and being incremented by 2 which prints even numbers obviously within 10. we need 11 too. If we change to 12, in place of 10 then this loop will print the exact same output needed.

3. In the third loop,
for i in range(3,11,2):
if i%2 == 0:
print(i,end=" ")
This will not print anything, the thing is we are using a for loop to fetch all the odd numbers and inside the loop, we are checking for even case and then printing if the number is even. so this will never print anything.

4. In the fourth loop,
for i in range(3,12,2):
print(i,end=" ")
This will print the exact same output, which is 3 5 7 9 11 since we are fetching only the odd numbers with an increment of 2 starting from 3 to 12(12 is excluded anyway)

Please check the compiled program and its output for your reference:
main.py 1 print(Loop 1) 2 - for i in range(3,11,1): 3 if i%2 != 0: 4 print(i,end= ) 5 6 print(\n\nLoop 2) 7 for i in ra
Output:
input Loop 1 3 5 7 9 Loop 2 3 5 7 9 Loop 3 Loop 4 3 5 7 9 11 ... Program finished with exit code 0 Press ENTER to exit consol
(Feel free to drop me a comment, If you need any help)

Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...

Add a comment
Know the answer?
Add Answer to:
python programming Which of the following code will print all odd numbers between 3 and 11,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT