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:
.selections
Exercise:
Programming Excercises
Question:12 | ISBN:978013274719 | Edition: 6

Question

(Check a number) Write a program that prompts the user to enter an integer and checks whether the number is divisible by both 5 and 6, divisible by 5 or 6, or just one of them (but not both). Here is a sample run:

Enter an integer: 10
Is 10 divisible by 5 and 6? False
Is 10 divisible by 5 or 6? True
Is 10 divisible by 5 or 6, but not both? True
 

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

SOURCE CODE:

# Enter an integer
a=eval(input("Enter an integer: "))
# Checking the given number is divible by 5 and 6
if a % 5 == 0 and a % 6 == 0:
    print("Is" + str(a) + "divisible by 5 and 6?")
    print("True")
else:
    print("Is" + str(a) + "divisible by 5 and 6?")
    print("False")
# Checking the given number is divisble by 5 or 6    
if a % 5 == 0 or a % 6 == 0:
    print("Is" + str(a) + "divisible by 5 or 6?")
    print("True")
else:
    print("Is" + str(a) + "divisible by 5 or 6?")
    print("False")
# Checkin the given number is divisible by 5 or 6 but not both    
if (a % 5 == 0 or a % 6 == 0) and not (a % 5 == 0 and a % 6 == 0):
    print("Is" + str(a) + "divisible by 5 or 6, but not both?")
    print("True")
else:
    print("Is" + str(a) + "divisible by 5 or 6, but not both?")
    print("False")

OUTPUT:



Enter an integer: 10                                                                                      
Is 10 divisible by 5 and 6?                                                                                 
False                                                                                                     
Is 10 divisible by 5 or 6?                                                                                  
True                                                                                                      
Is 10 divisible by 5 or 6, but not both?                                                                    
True 

 

0 0

Discussions

Post the discussion to improve the above solution.