w3resource

Python: Biggest even number between two numbers inclusive


Largest Even Number in Range

Write a Python program to find the biggest even number between two numbers inclusive.

Input:
m = 12
n = 51
Output:
50
Input:
m = 1
n = 79
Output:
78
Input:
m = 47
n = 53
Output:
52
Input:
m = 100
n = 200
Output:
200

Visual Presentation:

Python: Biggest even number between two numbers inclusive.
Python: Biggest even number between two numbers inclusive.

Sample Solution-1:

Python Code:

# Define a function named 'test' that takes two parameters, m and n
def test(m, n):
    # Check if m is greater than n or if both m and n are odd
    if m > n or (m == n and m % 2 == 1):
        # Return -1 if the condition is met
        return -1
    # Return n if n is even, otherwise return n - 1
    return n if n % 2 == 0 else n - 1

# Set values for m and n
m = 12
n = 51
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the given values of m and n
print(test(m, n))

# Set new values for m and n
m = 1
n = 79
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the new values of m and n
print(test(m, n))

# Set another set of values for m and n
m = 47
n = 53
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the new values of m and n
print(test(m, n))

# Set additional values for m and n
m = 100
n = 200
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the additional values of m and n
print(test(m, n))

Sample Output:

Biggest even number between 12 and 51
50

Biggest even number between 1 and 79
78

Biggest even number between 47 and 53
52

Biggest even number between 100 and 200
200

Flowchart:

Flowchart: Python - Biggest even number between two numbers inclusive.

Sample Solution-2:

Python Code:

# Define a function named 'test' that takes two parameters, p and q
def test(p, q):
    # Initialize a variable 'n' with the value of 'q'
    n = q
    # Continue looping while the least significant bit of 'n' is 1 (n is odd)
    while (n & 1) == 1:
        # Decrement 'n' by 1
        n -= 1
        # Check if 'n' has become less than 'p'
        if n < p:
            # Return -1 if the condition is met
            return -1
    # Return the final value of 'n'
    return n

# Set values for m and n
m = 12
n = 51
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the given values of m and n
print(test(m, n))

# Set new values for m and n
m = 1
n = 79
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the new values of m and n
print(test(m, n))

# Set another set of values for m and n
m = 47
n = 53
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the new values of m and n
print(test(m, n))

# Set additional values for m and n
m = 100
n = 200
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the additional values of m and n
print(test(m, n))

Sample Output:

Biggest even number between 12 and 51
50

Biggest even number between 1 and 79
78

Biggest even number between 47 and 53
52

Biggest even number between 100 and 200
200

Flowchart:

Flowchart: Python - Biggest even number between two numbers inclusive.

For more Practice: Solve these Related Problems:

  • Write a Python program to compute the largest even number between two given bounds by decrementing from the upper bound.
  • Write a Python program to use a while loop to find the maximum even integer in a given inclusive range.
  • Write a Python program to generate a list of even numbers in the range and then select the maximum value.
  • Write a Python program to implement a function that returns the largest even number between m and n using list comprehension.

Go to:


Previous: Sum of the magnitudes of the elements in the array with product signs.
Next: A valid filename.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.