📜  在给定范围内查找可被 7 和 5 的倍数整除的数的Python程序

📅  最后修改于: 2022-05-13 01:55:26.597000             🧑  作者: Mango

在给定范围内查找可被 7 和 5 的倍数整除的数的Python程序

给定一个数字范围,任务是编写一个Python程序来找出能被 7 和 5 的倍数整除的数字。

例子:

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.

通过分别对数字与 7 和 5 进行模运算,然后检查余数,可以检查一组整数是否可以被 7 和 5 整除。这可以通过以下方式完成:

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


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


输出:

35  is divisible by 7 and 5.

这也可以通过检查数字是否可以被 35 整除来完成,因为 7 和 5 的 LCM 是 35,任何可被 35 整除的数字都可以被 7 和 5 整除,反之亦然。

蟒蛇3

# 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

输出:

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