FizzBuzz Range

FizzBuzz Range

Medium Python Control Flow 13 views
Explanation Complexity

Problem Statement

Read integer n, for i from 1 to n output on new line: Fizz if i divisible by 3, Buzz if divisible by 5, FizzBuzz if divisible by both, else output i.

Input Format

First line: Integer n

Output Format

• For each number i from 1 to n, print on a new line:

• FizzBuzz if i is divisible by both 3 and 5

• Fizz if i is divisible by 3

• Buzz if i is divisible by 5

• Otherwise print i

Example

5
1
2
Fizz
4
Buzz

Constraints

• 1 ≤ n ≤ 10^5

Concept Explanation

For n = 5:

• 1 → not divisible by 3 or 5 → print 1

• 2 → not divisible by 3 or 5 → print 2

• 3 → divisible by 3 → print Fizz

• 4 → not divisible by 3 or 5 → print 4

• 5 → divisible by 5 → print Buzz

Step-by-Step Explanation

1.Read integer n.

2.Loop from i = 1 to n:

• If i % 3 == 0 and i % 5 == 0, print "FizzBuzz".

• Else if i % 3 == 0, print "Fizz".

• Else if i % 5 == 0, print "Buzz".

• Otherwise, print i.

Concept Explanation

For n = 5:

• 1 → not divisible by 3 or 5 → print 1

• 2 → not divisible by 3 or 5 → print 2

• 3 → divisible by 3 → print Fizz

• 4 → not divisible by 3 or 5 → print 4

• 5 → divisible by 5 → print Buzz

Step-by-Step Explanation

1.Read integer n.

2.Loop from i = 1 to n:

• If i % 3 == 0 and i % 5 == 0, print "FizzBuzz".

• Else if i % 3 == 0, print "Fizz".

• Else if i % 5 == 0, print "Buzz".

• Otherwise, print i.

Input / Output Format

Input Format
First line: Integer n
Output Format
• For each number i from 1 to n, print on a new line:

• FizzBuzz if i is divisible by both 3 and 5

• Fizz if i is divisible by 3

• Buzz if i is divisible by 5

• Otherwise print i
Constraints
• 1 ≤ n ≤ 10^5

Examples

Input:
5
Output:
1 2 Fizz 4 Buzz

Example Solution (Public)

Python
import sysns=sys.stdin.read().strip()nif not s: sys.exit(0)nn=int(s)nout=[]nfor i in range(1,n+1):n  if i%15==0:n    out.append('FizzBuzz')n  elif i%3==0:n    out.append('Fizz')n  elif i%5==0:n    out.append('Buzz')n  else:n    out.append(str(i))nsys.stdout.write('
'.join(out))

Official Solution Code

import sysns=sys.stdin.read().strip()nif not s: sys.exit(0)nn=int(s)nout=[]nfor i in range(1,n+1):n  if i%15==0:n    out.append('FizzBuzz')n  elif i%3==0:n    out.append('Fizz')n  elif i%5==0:n    out.append('Buzz')n  else:n    out.append(str(i))nsys.stdout.write('
'.join(out))
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.