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:
Output:
(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...
python programming Which of the following code will print all odd numbers between 3 and 11,...
In Python! Create the program that print out only odd numbers 1,3,5,7,9 Use either a while loop or a for loop to print only odd numbers 1, 3, 5, 7, 9 *tip: you can use range () function, or a condition with ‘break’ (e.g, while count < 10) In python!: Create a program that print out 12 months of a year and associated numbers (e.g., January 1, February 2…). Two lists should be created, and then loop through the list...
Write a python function that prints all odd numbers between two given numbers print_odd_numbers(10,20) → 11 13 15 17 19 print_odd_numbers(4,12) → 5 7 9 11 ### Your code here def print_odd_numbers(n1, n2):
Write the PYTHON code that displays the following numbers: The program should have a header that displays "Number Doubled Tripled" above the start of the numbers. If putting all numbers on the same line, then they will need to be separated so you can see he individual number.(https://www.programiz.com/python-programming/methods/built-in/print) The program should display Every whole number from 1 through 35. The program should display that number doubled. The program should also display that number tripled. Make a working version of this...
Need some help I am not understanding this programming class at all. We are using Microsoft visual studio with python in console mode to complete these labs. Double-click to hide white space CIS115 Week 4 Lab Overview Title of Lab: Multiplication Table in Python Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the user for the size of the multiplication table (from 2x2 to 10x10). Use a validation loop to...
Python 3 Write a program that calculates the factorial of the greatest odd number in a sequence of positive numbers (separated by spaces). The program must receive sequences of integers, one per line. The end of the input cases will be indicated by the number -1. For each line the program must print the factorial of the greatest odd number in each sequence. Note that for this question, there is always a greatest positive odd number in each line. Sample...
Java Programming. Write your own source code with comments. (Print distinct numbers) Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct...
PYTHON PROGRAMMING: DO NOT USE INPUT FUNCTION, use sys.argv Write a program that generates two random integer numbers between 1 and 100. Then, print out the numbers between the two randomly generated ones using a while loop. The output is shown below. Output: a) start:4, end:14 4 5 6 7 8 9 10 11 12 13 14 b) start:15, end:20 15 16 17 18 19 20
Write a Python program that calculates the product of all odd numbers between 1 and 20 (1x3x5x7x9x11x13x15x17x19), using the for loop and the range function.
python format: write a program that displays, 10 numbers per line, all the numbers from 100 to 200 that are divisible by 5 or 6, but not both. The numbers are separated by exactly one space My Code: count = 0 for i in range (100, 201): if (i %6==0 and i %5!=0) or (i%5==0 and i%6!=0): print(str(i), "") count = count + 1 if count == 10: string = str(i) + str("") print(string)...
python print ("num=", num) 12. (2) The following loop should print the squares of the odd numbers never ends. What is the error and how could it be fixed? num = 1 while num != 12: print (num ** 2) num += 2