Find Even Number Between two Given Numbers Using While Loop in Python.

We are going to write a program to print the even numbers between two given numbers using a while loop in python.

This program prints all the even numbers between two given numbers using a while loop in python.

    Simple code:

    start = int(input("Enter the start number: "))
    end = int(input("Enter the end number: "))
    
    while(start <= end):
        if(start % 2 == 0):
            print(start)
        start += 1

    Output:

    Enter the start number: 2
    Enter the end number: 9
    2
    4
    6
    8

    Code with explanation:

    # Ask the user to enter the start and end numbers
    start = int(input("Enter the start number: "))
    end = int(input("Enter the end number: "))
    
    # Use a while loop to iterate through all the numbers between start and end (inclusive)
    while(start <= end):
        # Check if the current number is even using the modulus operator
        if(start % 2 == 0):
            # Print the even number
            print(start)
            
        # Move to the next number
        start += 1

    Explanation:

    • The program prompts the user to enter the start and end numbers using the input() function and stores them in variables ‘start’ and ‘end’.
    • A while loop is used to iterate through all the numbers between the start and end (inclusive). At each iteration, the current number is checked if it is even using the modulus operator. If it is even, it is printed using the print() function.
    • Finally, the loop moves to the next number by incrementing the ‘start’ variable by 1.

    Discover more from Python Mania

    Subscribe to get the latest posts sent to your email.

    0 0 votes
    Article Rating
    Subscribe
    Notify of
    0 Comments
    Most Voted
    Newest Oldest
    Inline Feedbacks
    View all comments

    Related Articles:

    Recent Articles:

    0
    Would love your thoughts, please comment.x
    ()
    x

    Discover more from Python Mania

    Subscribe now to keep reading and get access to the full archive.

    Continue reading