Check if number is divisible by 3 using python if-else


In this tutorial, you will learn how to write a Python program that can check if a given number is divisible by 3 or not. This program uses if-else statements for this purpose.


The program works by taking a number as input from the user. Then uses if-else statements to check if the given number leaves a remainder when divided by 3. Lastly, it displays a suitable message showing the result.

Key concepts you need to know beforehand

  • Variables: Familiarity with variables is important for storing and dividing numbers.
  • Mathematical operators: Knowledge of mathematical operators such as division (/) and modulus (%) is necessary.
  • If-else statements: Understanding the if-else construct is vital for this program.
  • Comparison operators: Familiarity with comparison operators such as equality (==) and inequality (!=).
  • Input/output: Knowing how to take input from the user and display output is helpful.

Problem Statement

Create a Python program to determine whether a given number is divisible by 3 or not. Your program should take a number as user input and then divide it by 3 to check its remainder. If the remainder is zero then it is divisible by 3 otherwise it is not divisible. For both cases, it needs to display an appropriate message.

Example:

Suppose the user runs the program and enters the number 27 as input. Then the program should run in this way:

Enter the number to be checked: 27
​
Output: The number 27 is divisible by 3.

In the above example, the program prompts the user to enter a number. After receiving the input, the program performs the check by using the if-else statements and the modulus operator (%).

Since 27 is divisible by 3 and do not leave a remainder, the program outputs the message “The number 27 is divisible by 3.”

Python Program to check if a given number is divisible by 3 or not

# Divisibility by 3 Checker
​
# User input
num = int(input("Enter a number: "))
​
# Divisibility check
if num % 3 == 0:
    print("The number", num , "is divisible by 3.")
else:
    print("The number", num , "is not divisible by 3.")

Output:

Enter a number: 25
The number 25 is not divisible by 3.

Explanation of the program

The program starts by asking the user to enter a number and stores the input inside the variable num. The input() function is used to receive the input from the user, and int() is used to convert the input to an integer.

# Divisibility by 3 Checker
​
# User input
number = int(input("Enter a number: "))

After receiving the number from the user, the program performs the divisibility check. The if-else statements used here execute different code blocks based on different conditions.

The condition num % 3 == 0 checks if the remainder of the variable num when divided by number 3 is equal to zero. If the condition is True, the program prints the message "The number <num> is divisible by 3.", where <num> is the user input.

if num % 3 == 0:

    print("The number", num, "is divisible by 3.")

If condition is False, it means that the number is not divisible by 3 and leaves a remainder. In this case, the else block is executed, and the program prints the message "The number <num> is not divisible by 3.".

else:
    print("The number", num , "is not divisible by 3.")

That’s it! Run this program and test it with different numbers to check the output and verify the divisibility by 3.

Conclusion

In this article, we learned how to write a Python program that checks if a number is divisible by 3 or not. The program uses the modulus operator (%) to determine if the remainder of dividing the number by 3 is zero or not. Then using if-else statements, the program provides an appropriate message.

You also learned some core concepts of Python in this article. Concepts such as variables and mathematical operators make the basic building block of any program. Likewise, concepts such as if-else statements and comparison operators are important as well. All these concepts form the building blocks of the program and are essential to grasp the logic and flow of the code.

Happy leaning!