write the programe to check whether a number is divisible by 5 or not in python
Answers
In other words, we want to check if a number is a multiple of 5.
If you generally just divide the number with 5, you would get a decimal or a fraction and stuff. But we do not want to do that if a number is divisible by 5.
Only integers, not decimals should be divisible by 5.
But taking that into consideration, we can not write all the multiples of five and check if the input matches it.
Hence, we will have to think of a way around it. If we notice, if the number if divided by 5, it would leave a remainder of 0.
And using this, we could check for the remainder and tell whether if it is a factor or not.
Here is the code:
number = int(input())
if number%5 == 0:
print('Yes, %d is a factor of 5'%(number))
else:
print('No, %d is not a factor of 5' %(number))
The '%' or modulo returns the remainder.
That is exactly how you solve the problem :)
The following codes must be typed in script mode, saved and then executed.
Here, we'll be using the IF - ELSE function. It is a function that decides the result based on a given condition.
It will run the body of the code, only if the condition is true. Otherwise, the 'else' code of the value inputted will be executed.
CODE:
num = int(input("Enter a number:"))
if num%5 == 0:
print("The number entered is divisible by 5.")
else:
print("The number entered isn't divisible by 5.")
OUTPUT: