How to Print All Numbers in a Range Divisible by a Given Number in Python

In this tutorial, we will learn how to program "How to Print All Numbers in a Range Divisible by a Given Number in Python". The objective is to print all numbers within a range that are divisible by a given number. This tutorial will guide you step by step through the process of finding those divisible numbers. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving skills and improve your coding abilities.

This topic is straightforward and easy to understand. Simply follow the instructions provided, and you will complete it with ease. The program will guide you step by step through the process of finding the numbers within a range that are divisible by a given number. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. while True:
  2.     print("\n=========== Print All Numbers in a Range Divisible by a Given Number ===========\n")
  3.  
  4.     lower = int(input("Enter lower range limit: "))
  5.     upper = int(input("Enter upper range limit: "))
  6.     n = int(input("Enter the number to be divided by: "))
  7.  
  8.     print(f"\nNumbers between {lower} and {upper} divisible by {n} are:")
  9.     for i in range(lower, upper + 1):
  10.         if i % n == 0:
  11.             print(i)
  12.  
  13.     # Ask user if they want to try again (AFTER the loop)
  14.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  15.     if opt == 'no':
  16.         print("Exiting program...")
  17.         break
  18.     elif opt != 'yes':
  19.         print("Invalid choice. Exiting program...")
  20.         break

This Python program prints all numbers within a user-specified range that are divisible by a given number. It prompts the user to input a lower and upper range limit and the divisor. Using a `for` loop, it iterates through the range and prints only the numbers divisible by the specified divisor. After displaying the results, the program asks the user if they want to repeat the process, allowing multiple calculations in a loop until the user chooses to exit.

Output:

There you have it we successfully created How to Print All Numbers in a Range Divisible by a Given . I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials