Write a block of Python code that prompts the user to enter an integer. If the n
ID: 3723722 • Letter: W
Question
Write a block of Python code that prompts the user to enter an integer. If the number is evenly divisible by 2, output to the screen “This number is divisible by 2.” If the number is evenly divisible by 3, output to the screen “This number is divisible by 3.” However, if the number is evenly divisible by both 2 and 3, output to the screen “This number is divisible by both 2 and 3.” If the number is not divisible by 2 or 3, then output to the screen “This number is not divisible by 2 or 3.” Note: there should be only one line of output on the screen after the input.
Explanation / Answer
##CODE IS PLAINED IN COMMENTS
number=input('Enter a Integer:') # Take input from user
i=number%2 #take the remainder divided by 2 and if number is divisible by 2,then remainder will be 0
j=number%3 #take the remainder divided by 3 and if number is divisible by 3,then remainder will be 0
if i==0 and j==0: # so if i and j both are 0,then it is divisible by 2 and 3
print('This number is divisible by both 2 and 3')
elif i!=0 and j!=0: #If i and j is not equals to zero,that indicates both are not divisble by 2 and 3
print('This number is not divisible by 2 or 3')
elif i==0: #In case if only i is equal to zero, that indicates it is divisible by 0
print('This number is divisible by 2')
else: #else the last possible case would be if j is equal to 0--no need to write the condition since we have passed all the cases except this one.SO that indicates j is divisble by as it should be zero if it passes through all the above conditions
print('This number is divisible by 3')
#Output for different cases:
Enter a Integer:6
This number is divisible by both 2 and 3
Enter a Integer:8
This number is divisible by 2
Enter a Integer:9
This number is divisible by 3
Enter a Integer:11
This number is not divisible by 2 or 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.