Monday, November 18, 2024
Google search engine
HomeLanguagesPython Program to Find Numbers Divisible by 7 and Multiple of 5...

Python Program to Find Numbers Divisible by 7 and Multiple of 5 in a Given Range

Given a range of numbers, the task is to write a Python program to find numbers divisible by 7 and multiple of 5.

Example:

Input:Enter minimum 100
Enter maximum 200

Output:
105  is divisible by 7 and 5.
140  is divisible by 7 and 5.
175  is divisible by 7 and 5.


Input:Input:Enter minimum 29
Enter maximum 36

Output:
35  is divisible by 7 and 5.

Algorithm -1

  1. Input the minimum and maximum range from the user.
  2. Compute the minimum and maximum multiples of 35 (7*5) that are greater than or equal to the minimum and less than or equal to the maximum, respectively. This is done by dividing the minimum and maximum by 35, rounding up or down using the ceil and floor functions in the math library, and then multiplying by 35 to get the actual multiples.
  3. Generate the desired numbers by creating a range object that starts at the minimum multiple, ends at the maximum multiple, and has a step size of 35 (since the desired numbers are multiples of 35).
  4. If the range object is not empty, print the numbers in the range with a newline separator. Otherwise, print a message indicating that no numbers were found.

Python3




import math
 
minimum = 100
maximum = 200
 
min_multiple = math.ceil(minimum / 35) * 35
max_multiple = math.floor(maximum / 35) * 35
 
result = range(min_multiple, max_multiple+1, 35)
 
if result:
    print(*result, sep='\n')
else:
    print("No numbers found.")


Output

105
140
175

Time complexity: O(1) – the time complexity is constant

Auxiliary space: O(1) – only constant space is used.

A set of integers can be checked for divisibility by 7 and 5 by performing the modulo operation of the number with 7 and 5 respectively, and then checking the remainder. This can be done in the following ways : 

Python3




# enter the starting range number
start_num = int(29)
 
# enter the ending range number
end_num = int(36)
 
# initialise counter with starting number
cnt = start_num
 
# check until end of the range is achieved
while cnt <= end_num:
   
    # if number divisible by 7 and 5
    if cnt % 7 == 0 and cnt % 5 == 0:
        print(cnt, " is divisible by 7 and 5.")
         
    # increment counter
    cnt += 1


Output

35  is divisible by 7 and 5.

This can also be done by checking if the number is divisible by 35, since the LCM of 7 and 5 is 35 and any number divisible by 35 is divisible by 7 and 5 and vice versa also. 

Python3




# enter the starting range number
start_num = int(68)
 
# enter the ending range number
end_num = int(167)
 
# initialise counter with starting number
cnt = start_num
 
# check until end of the range is achieved
while cnt <= end_num:
 
    # check if number is divisible by 7 and 5
    if(cnt % 35 == 0):
        print(cnt, "is divisible by 7 and 5.")
 
    # incrementing counter
    cnt += 1


Output

70 is divisible by 7 and 5.
105 is divisible by 7 and 5.
140 is divisible by 7 and 5.

The time complexity is O(n), where n is the number of integers in the given range from start_num to end_num.

The auxiliary space is O(1), as the amount of memory used by the code remains constant regardless of the size of the input.

Method: find numbers divisible_by 7 and multiple of 5 in_range

  1. Take input for the minimum and maximum values of the range from the user using the input() function and store them in variables min_val and max_val, respectively.
  2. Use a for loop to iterate over each number within the range min_val to max_val (inclusive) using the range() function.
  3. For each number in the loop, check if it is divisible by 7 and a multiple of 5 by using the modulo operator %.
  4. If the number satisfies both conditions, print it along with a message saying that it is divisible by 7 and 5.
  5. The program ends when all numbers in the range have been checked.

Python3




# Take input for minimum and maximum values
min_val = 100
max_val = 200
 
# Check each number within the range
for num in range(min_val, max_val+1):
    if num % 7 == 0 and num % 5 == 0:
        print(num, "is divisible by 7 and 5.")


Output

105 is divisible by 7 and 5.
140 is divisible by 7 and 5.
175 is divisible by 7 and 5.

The time complexity of this program is O(n),

The auxiliary space of this program is O(1).

List Comprehension Approach:

  1. Take the minimum and maximum values as input from the user.
  2. Initialize an empty list to store the numbers that are divisible by 7 and multiple of 5.
  3. Use list comprehension to filter out the numbers that satisfy the condition.
  4. Print the list of numbers that are divisible by 7 and multiple of 5.

Python3




# Python program to find numbers divisible
# by 7 and multiple of 5 in a given range
 
# Step 1: Take input from the user
minimum = 100
maximum = 200
 
# Step 2: Initialize an empty list
result_list = []
 
# Step 3: Use list comprehension to
# filter out the numbers
result_list = [num for num in range(
    minimum, maximum+1) if num % 7 == 0 and num % 5 == 0]
 
# Step 4: Print the result list or a
# message indicating no such numbers exist
if result_list:
    print("Numbers divisible by 7 and multiple of 5 in the given range are: ")
    for num in result_list:
        print(num, "is divisible by 7 and 5.")
else:
    print("There are no numbers divisible by 7 and multiple of 5 in the given range.")


Output

Numbers divisible by 7 and multiple of 5 in the given range are: 
105 is divisible by 7 and 5.
140 is divisible by 7 and 5.
175 is divisible by 7 and 5.

Time Complexity: O(n), where n is the range of numbers from the minimum to maximum value.

Auxiliary Space: O(k), where k is the number of numbers that are divisible by 7 and multiple of 5 in the given range.

RELATED ARTICLES

Most Popular

Recent Comments