Python program to check if a number is divisible by another number or not

Python program to check if a number is divisible by another number or not:

In this post, we will learn how to check if a number is divisible by another number or not in Python. You will learn how we can use the modulo operator or % to check if a number is divisible by another number.

A number x is divisible by another number y if the remainder of x/y is 0 i.e. if x is divided by y, the remainder will be 0. We can also say that x is perfectly divisible by y.

This post will show you:

  • How to check if a number is divisible by another number in Python.
  • How to check if a number is divisible by 2, 3, or 5 in Python
  • How to check if the numbers of a list are divisible by another number.
  • Find all numbers in a range divisible by a number.

Modulo operator:

The symbol % is called modulo operator in Python. It is used with two operands. For example, if x and y are two numbers, x%y will return the remainder of x/y. So, if the result of x%y is 0, x is perfectly divisible by y.

Example 1: Python program to check if a number is divisible by another number:

This program will take two numbers as inputs from the user and print a message if one number is perfectly divisible by another or not.

first_num = int(input('Enter the first number: '))
second_num = int(input('Enter the second number: '))

if first_num % second_num == 0:
    print(f'{first_num} is divisible by {second_num}')
else:
    print(f'{first_num} is not divisible by {second_num}')

Here,

  • first_num and second_num are two variables to hold the user input numbers. The input method reads the user entered values. We have to convert the return value of input to integers as it returns string values.
  • The if-else block is using the modulo operator to check if the first_num is perfectly divisible by the second_num or not. If it returns 0, then it prints a message that the first_num is divisible by second_num. Else, it enters to the else block and prints that it is not divisible.

If you run this program, it will print output as below:

Enter the first number: 12
Enter the second number: 2
12 is divisible by 2

Enter the first number: 12
Enter the second number: 5
12 is not divisible by 5

Example 2: How to check if a number is divisible by 2, 3, or 5 in Python:

Let’s write a program that will find if a number is divisible by 2, 3 or 5 in Python. Similar to the above program, it will take one number as input from the user and print one message if it is divisible by 2, 3, or 5.

given_num = int(input('Enter the number: '))

if given_num % 2 == 0:
    print(f'{given_num} is divisible by 2')
elif given_num % 3 == 0:
    print(f'{given_num} is divisible by 3')
elif given_num % 5 == 0:
    print(f'{given_num} is divisible by 5')
else:
    print(f'{given_num} is not divisible by 2, 3 or 5')
  • In this program, the user-entered number is stored in the variable given_num.
  • The if-elseif-else method checks if it is divisible by 2, 3 or 5. It prints a message based on the user input.

If you run this program, it will print output as below:

Enter the number: 5
5 is divisible by 5

Enter the number: 12
12 is divisible by 2

Enter the number: 9
9 is divisible by 3

Enter the number: 25
25 is divisible by 5

Enter the number: 23
23 is not divisible by 2, 3 or 5

We can also use a or statement to check if the number is divisible by any of 2, 3 or 5.

given_num = int(input('Enter the number: '))

if given_num % 2 == 0 or given_num % 3 == 0 or given_num % 5 == 0:
    print(f'{given_num} is divisible by 2, 3 or 5')
else:
    print(f'{given_num} is not divisible by 2, 3 or 5')

The if block checks if the number is divisible by 2, 3 or 5. Else, it moves to the else block. Based on the result, it prints a message.

It will print output as below: Python check if a number is divisible by 2,3 or 5

Example 3: How to check if the numbers of a list are divisible by another number:

We can write a program as below to find out all the numbers divisible by a number in a list:

num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result_list = [x for x in num_list if x % 3 == 0]

print(result_list)

In this program, result_list will hold all the numbers of num_list which are divisible by 3.

[3, 6, 9]

Example 4: Find all numbers in a range divisible by a number:

We can use a loop to iterate in a range and find out all the numbers which are divisible by a specific number by using the modulo operator. The below program takes the lower and upper ranges of the limit as inputs from the user and prints all numbers divisible by 5:

lower_range = int(input("Enter the lower range: "))
upper_range = int(input("Enter the upper range: "))

for i in range(lower_range, upper_range):
    if(i % 5 == 0):
        print(i)

The lower range is stored in the variable lower_range and the upper range is stored in upper_range. The for loop runs in the lower and upper range and prints all values divisible by 5.

It will print output as below:

Enter the lower range: 10
Enter the upper range: 45
10
15
20
25
30
35
40

Or, you can use list comprehension to create a list with the results:

lower_range = int(input("Enter the lower range: "))
upper_range = int(input("Enter the upper range: "))

result = [x for x in range(lower_range, upper_range) if x % 5 == 0]
print(result)

It will print:

Enter the lower range: 10
Enter the upper range: 45
[10, 15, 20, 25, 30, 35, 40]

You might also like: