Numbers Divisible by 7

Print numbers between a and b divisible by 7.

IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 3 of 13
numbers-divisible-by-7.py
1# Take range
2a = int(input("Enter a: "))
3b = int(input("Enter b: "))
4
5# Print divisible by 7
6print(f"Numbers divisible by 7 between {a} and {b}:")
7for i in range(a, b + 1):
8 if i % 7 == 0:
9 print(i, end=" ")
10print()

Output

Enter a: 10
Enter b: 50
Numbers divisible by 7 between 10 and 50:
14 21 28 35 42 49

What's going on

Check each number in range for divisibility.

Key Concepts:

Loop through range a to b
Check if divisible by 7 using modulo
Print if condition met