DEV Community

Bala Murugan
Bala Murugan

Posted on

Problem: Multiples of 3 and 5

Definition

This program is used to print all numbers from 1 to n that are divisible by both 3 and 5. A number is considered a multiple of 3 and 5 if it is exactly divisible by both numbers without any remainder.

Algorithm

Step 1: Start

Step 2: Input n

Step 3: Initialize i = 1

Step 4: Check whether i <= n

Step 5: If i is divisible by both 3 and 5, print i

Step 6: Increment i by 1

Step 7: Repeat Steps 4 to 6 until i > n

Step 8: Stop.

Flowchart:

    ┌───────┐
    │ Start │
    └───┬───┘
        │
        ▼
  ┌──────────┐
  │ Input n  │
  └────┬─────┘
       │
       ▼
  ┌──────────┐
  │  i = 1   │
  └────┬─────┘
       │
       ▼
   ┌────────┐
   │ i <= n │
   └──┬───┬─┘
     Yes  No
      │    │
      ▼    ▼
Enter fullscreen mode Exit fullscreen mode

┌────────────────┐
│ i%3==0 and │
│ i%5==0 ? │
└──┬─────────┬───┘
Yes No
│ │
▼ │
┌────────┐ │
│ Print i│ │
└────┬───┘ │
│ │
▼ │
┌────────┐ │
│ i=i+1 │◄────┘
└────┬───┘

└──────► Back to i <= n

        No
         │
         ▼
    ┌──────┐
    │ Stop │
    └──────┘
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 = i + 1
Enter fullscreen mode Exit fullscreen mode
Input:

Enter n: 30
Enter fullscreen mode Exit fullscreen mode
Output:

15
30
Enter fullscreen mode Exit fullscreen mode

Top comments (0)