Check Numbers Divisible by Another Number in Python

Welcome to our tutorial on how to check numbers divisible by another number in Python! In this tutorial, we will be discussing how to use the built-in “input()” function, the modulus operator, and the “if-else” statement to check (or Find ) if a given number is divisible by another number.

Python Program to Check First Numbers Divisible by Another Number

# Python Program to Check First Numbers Divisible by Another Number

# we are taking a number from user as input
# entered value will be converted to int from string
numn = int(input("Enter a Number (Numerator): "))
numd = int(input("Enter a Number (denominator): "))

if numn%numd==0:
  print("\n" +str(numn)+ " is divisible by " +str(numd))
else:
  print("\n" +str(numn)+ " is not divisible by " +str(numd))

Step 1: Prompt User for Input

The first step in the code to check numbers divisibility by another number  is to prompt the user to enter two numbers – a numerator and a denominator. This is done using the input() function, which displays a message to the user and waits for them to enter a value. In this case, the message is “Enter a Number (Numerator): ” and “Enter a Number (Denominator): “. 

Step 2: Convert User Input to Integer

Once the user has entered the numerator and denominator, the values are converted from strings to integers using the int() function. The input is being taken from the user and stored in the variables “numn” and “numd”.

The input function returns the input as a string, so it is important to convert the value to int before using it in further calculations. This is necessary because we will be performing mathematical operations on these values, and Python cannot perform arithmetic on strings.

Step 3: Check if the Number is Divisible

The main logic of the code comes next. Once we have the two numbers, we can use the modulus operator(%) to check if the numerator is divisible by the denominator. The modulus operator returns the remainder of the division of the first number by the second number. If the remainder is 0, that means the numerator is divisible by the denominator.

Step 4: Display the Result

Finally, we use an if statement to check whether the numerator is divisible by the denominator. If it is, we print a message that says so. If it isn’t, we print a different message that says the numerator is not divisible by the denominator.

That’s it! The code is fairly simple, but it demonstrates some important concepts in Python, such as taking user input, converting data types, and using conditional statements to control program flow.

Output:

If the remainder is zero, that means the numerator is divisible by the denominator, and the statement inside the if block will be executed.

If the remainder is not zero, that means the numerator is not divisible by the denominator, and the statement inside the else block will be executed.

Enter a Number (Numerator): 60
Enter a Number (denominator): 10
60 is divisible by 10

In conclusion, Checking numbers divisible by another number in Python is a simple task that can be achieved using the built-in “input()” function, the modulus operator, and the “if-else” statement. By understanding how to use these functions and operators, you’ll be able to check if a given number is divisible by another number in a more precise way.

Additionally, this example illustrates the use of input function, modulus operator, and the if-else statement in Python. With this tutorial, you now have a solid understanding of how to Check numbers divisible by another number in Python. By prompting the user to enter the numbers, we can ensure that the input is accurate and specific to the problem we are trying to solve.

Additionally, the code can be easily modified to include additional logic or operations, depending on the needs of the program.

Overall, this code provides a simple and efficient way to check whether one number is divisible by another in Python. By following the steps outlined in the code and understanding the underlying concepts, programmers can create more complex and robust programs that can perform a variety of mathematical and computational tasks.

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions

About The Author

Leave a Reply