DEV Community

Bala Murugan
Bala Murugan

Posted on

Multiples of 3 and 5

Definition:

This program is used to print all numbers between 1 and n that are divisible by both 3 and 5.

A number is a multiple of 3 and 5 if it leaves no remainder when divided by both numbers.

For example:

15 ÷ 3 = 5 and 15 ÷ 5 = 3
30 ÷ 3 = 10 and 30 ÷ 5 = 6

So, 15 and 30 are multiples of both 3 and 5.

Algorithm:

Step 1: Start
Step 2: Input n
Step 3: Set i = 1
Step 4: While i <= n
Step 5: If i % 3 == 0 and i % 5 == 0, print i
Step 6: Increment i by 1
Step 7: Repeat Steps 4 to 6
Step 8: Stop

Flow Chart:

      ┌─────────┐
      │  START  │
      └────┬────┘
           │
           ▼
    ┌─────────────┐
    │   Input n   │
    └──────┬──────┘
           │
           ▼
    ┌─────────────┐
    │    i = 1    │
    └──────┬──────┘
           │
           ▼
       ┌────────┐
       │ i <= n │
       └──┬──┬──┘
        Yes No
         │   │
         │   ▼
         │ ┌─────┐
         │ │ END │
         │ └─────┘
         │
         ▼
 ┌─────────────────┐
 │ i%3==0 and      │
 │ i%5==0 ?        │
 └──────┬────┬─────┘
      Yes    No
       │      │
       ▼      │
 ┌─────────┐  │
 │ Print i │  │
 └────┬────┘  │
      │       │
      └──┬────┘
         ▼
   ┌─────────┐
   │ i = i+1 │
   └────┬────┘
        │
        └──────────► Back to
                     i <= n
Enter fullscreen mode Exit fullscreen mode

code:

n = int(input("Enter n: "))
i = 1

while i <= n:
    if i % 3 == 0 and i % 5 == 0:
        print(i)
    i += 1
Enter fullscreen mode Exit fullscreen mode
OUTPUT:

15
30
45
Enter fullscreen mode Exit fullscreen mode

Top comments (0)