mindninjaX / Python-Projects-for-Beginners

135 stars 112 forks source link

Add Fizzbuzz project #3

Open Raashika0201 opened 2 years ago

vivekdeme commented 1 year ago

Write a Python program that iterates the integers from 1 to 50. For multiples of three print "Fizz" instead of the number and for multiples of five print "Buzz". For numbers that are multiples of three and five, print "FizzBuzz".

Solution:

Python Code : for fizzbuzz in range(51): if fizzbuzz % 3 == 0 and fizzbuzz % 5 == 0: print("fizzbuzz") continue elif fizzbuzz % 3 == 0: print("fizz") continue elif fizzbuzz % 5 == 0: print("buzz") continue print(fizzbuzz)

Sample Output: fizzbuzz 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz .....

41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz Flowchart:

Flowchart: Python program to get Fizz, Buzz and FizzBuzz

SudhA-2k25 commented 1 year ago

FIZZ BUZZ using Function

Multiples of 3 - Fizz

Multiples of 5 - Buzz

def fizzbuzz(n): result = ["FizzBuzz" if i % 3 == 0 and i % 5 == 0 else "Fizz" if i % 3 == 0 else "Buzz" if i % 5 == 0 else i for i in range(1, n+1)] return result

Test the fizzbuzz function

n = int(input("Enter the range :")) output = fizzbuzz(n) print(output)

AbhayXcoderx123 commented 1 year ago

def fizz_buzz(n):

Declare a list of strings to store the results

result = []

# Loop from 1 to n
for i in range(1, n + 1):

    # Check if i is divisible by both 3 and 5
    if i % 3 == 0 and i % 5 == 0:

        # Add "FizzBuzz" to the result list
        result.append("FizzBuzz")

    # Check if i is divisible by 3
    elif i % 3 == 0:

        # Add "Fizz" to the result list
        result.append("Fizz")

    # Check if i is divisible by 5
    elif i % 5 == 0:

        # Add "Buzz" to the result list
        result.append("Buzz")
    else:

        # Add the current number as a string to the
        # result list
        result.append(str(i))

# Return the result list
return result

n = 100

Call the fizz_buzz function to get the result

result = fizz_buzz(n)

Print the result

print(' '.join(result))