SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
.loops
Exercise:
Programming Excercises
Question:13 | ISBN:978013274719 | Edition: 6

Question

(Find numbers divisible by 5 or 6, but not both) Write a program that displays, ten 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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Find numbers divisible by 5 or 6, but not both Program code:

# Initialize variables
count = 0  # Keeps track of the number of valid numbers found
#Prompt output statement
print("Print ten numbers per line, all the numbers from 100 to 200 that are divisible by 5 or 6, but not both is as:\n")
# Iterate from 100 to 200 (inclusive)
for num in range(100, 201):
    # Check if the number is divisible by 5 or 6, but not both
    if (num % 5 == 0 or num % 6 == 0) and not (num % 5 == 0 and num % 6 == 0):
        # Print the number with a space
        print(num, end=" ")
        count += 1  # Increment the count

        # Check if ten numbers have been printed
        if count % 10 == 0:
            print()  # Print a new line after every ten numbers


# Print a new line if the last line doesn't have ten numbers
if count % 10 != 0:
    print()

Executed Output:

   
Print ten numbers per line, all the numbers from 100 to 200 that are divisible by 5 or 6, but not both is as:

100 102 105 108 110 114 115 125 126 130 
132 135 138 140 144 145 155 156 160 162 
165 168 170 174 175 185 186 190 192 195 
198 200 

 

0 0

Discussions

Post the discussion to improve the above solution.