Skip to content
Related Articles

Related Articles

Python – Find all pairs of consecutive odd positive integer smaller than a with sum greater than b

View Discussion
Improve Article
Save Article
  • Last Updated : 26 Mar, 2021

Given two positive integers a and b, the task is to write a program in python to find all pairs of consecutive odd numbers which are smaller than the first number a and their sum should be greater than the second number b

Examples:

Input:
a = 60
b = 100
Output:
Pairs of consecutive number are:
51 , 53
53 , 55
55 , 57
57 , 59

Input:
a = 20
b = 200
Output:
None

Approach:

Two numbers are given and then check if they are a positive integer and checked for the first number to be greater than the half of the second number. Then we will check for odd positive integer and assigned in a variable a. In the while statement, pairs of the odd consecutive integers are found and printed.

Example 1:

Python3




# input first and second number
a = 60
b = 100
  
print('a =', a)
print('b =', b)
  
# check the first number should be greater
# than the half of second number and both number
# should be positive integer
if(a > 0 and b > 0 and a > b/2):
  
    # to ensure value in firstNum variable
    # must be odd positive integer
    if(a % 2 == 0):
        a -= 1
    else:
        a -= 2
  
    b //= 2
    print("Pairs of consecutive number are:")
  
    # find the pairs of odd
    # consecutive positive integer
    while(b <= a):
        if(b % 2 != 0):
            x = b
            if(x + 2 <= a):
                print(x, ',', x+2)
  
        b += 1
else:
  print("None")

Output:

a = 60
b = 100
Pairs of consecutive number are:
51 , 53
53 , 55
55 , 57
57 , 59

Example 2:

Python3




# input first and second number
a = 20
b = 200
  
print('a =', a)
print('b =', b)
  
# check the first number should be greater
# than the half of second number and both number
# should be positive integer
if(a > 0 and b > 0 and a > b/2):
  
    # to ensure value in firstNum variable
    # must be odd positive integer
    if(a % 2 == 0):
        a -= 1
    else:
        a -= 2
  
    b //= 2
    print("Pairs of consecutive number are:")
  
    # find the pairs of odd
    # consecutive positive integer
    while(b <= a):
        if(b % 2 != 0):
            x = b
            if(x + 2 <= a):
                print(x, ',', x+2)
                 
  
        b += 1
else:
    print("None")

Output:

a = 20
b = 200
None

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!