Robotics & Artificial Intelligence

A Python program that takes a list of integers and tests the user's ability to:

  • write a code in Python.
  • use for loops for iteration to iterate over a list.
  • apply conditional statements to check divisibility.
  • print the results based on the condition.

A list of integers [10, 15, 24, 30, 20, 45, 25, 60] is provided.

The program counts and prints all numbers from the list that are divisible by 3 as well as 5.

The expected output:

The numbers divisible by 3 as well as 5 in the list are:
15
30
45
60
Number of elements divisible by 3 as well as 5: 4

An outline of the program is given below to make the task easier.

Now, fill in the blanks to complete the code. The program is given below:

# a program to display numbers divisible by 3 and 5 in the list
c = 0
My_List = [10, 15, 24, 30, 20, 45, 25, 60]
print("Given List:", My_List)
print("The numbers divisible by 3 as well as 5 in the list are:")
for ........... in My_List:                    # line 1
    if(num%3 == 0 ......... num%5 == 0):       # line 2
        print(num)
        c = ...............                    # line 3
print("Number of elements divisible by 3 as well as 5: ", c)

Python Control Flow

2 Likes

Answer

(a) Line 1: num

(b) Line 2: and

(c) Line 3: c+1

The complete program is given below:

# a program to display numbers divisible by 3 and 5 in the list
c = 0
My_List = [10, 15, 24, 30, 20, 45, 25, 60]
print("Given List:", My_List)
print("The numbers divisible by 3 as well as 5 in the list are:")
for num in My_List:                    # line 1
    if(num%3 == 0 and num%5 == 0):     # line 2
        print(num)
        c = c+1                        # line 3
print("Number of elements divisible by 3 as well as 5: ", c)

Answered By

1 Like


Related Questions