{"id":25711,"date":"2021-12-04T09:19:30","date_gmt":"2021-12-04T03:49:30","guid":{"rendered":"https:\/\/python-programs.com\/?p=25711"},"modified":"2021-12-04T09:19:30","modified_gmt":"2021-12-04T03:49:30","slug":"fizzbuzz-algorithm-and-its-code-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/fizzbuzz-algorithm-and-its-code-in-python\/","title":{"rendered":"FizzBuzz Algorithm and Its Code in Python"},"content":{"rendered":"
The FizzBuzz algorithm is a common coding interview topic. Fizz and Buzz are numbers that are multiples of three and five, respectively.<\/p>\n
In this article, we see how to develop the FizzBuzz algorithm using Python.<\/p>\n
A children’s game inspired the FizzBuzz algorithm. This method has long been one of the most popular coding interview tasks.<\/p>\n
You are given a range of numbers and must generate output using the following rules:<\/p>\n
This coding challenge is commonly encountered with digits 3 and 5, but the principle for solving the problem stays the same.<\/p>\n
Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" The FizzBuzz algorithm is a common coding interview topic. Fizz and Buzz are numbers that are multiples of three and five, respectively. In this article, we see how to develop the FizzBuzz algorithm using Python. FizzBuzz Algorithm A children’s game inspired the FizzBuzz algorithm. This method has long been one of the most popular coding …<\/p>\n\n
# Give the number(upper limit) as static input and store it in a variable.\r\ngvn_numb = 8\r\n# Loop from 1 to the given number using the for loop.\r\nfor itr in range(1, gvn_numb+1):\r\n # Check if the iterator value is divisible by 3 and 5 using the modulus operator\r\n # and if conditional statement.\r\n if itr % 3 == 0 and itr % 5 == 0:\r\n # If it is true, then print \"FizzBuzz\".\r\n print(itr, \" - FizzBuzz\")\r\n # Again Check if the iterator value is divisible by 3 using the modulus operator\r\n # and elif conditional statement.\r\n elif itr % 3 == 0:\r\n # If it is true, then print \"Fizz\".\r\n print(itr, \" - Fizz\")\r\n # Check if the iterator value is divisible by 5 using the modulus operator\r\n # and elif conditional statement.\r\n elif itr % 5 == 0:\r\n # If it is true, then print \"Buzz\".\r\n print(itr, \" - Buzz\")\r\n else:\r\n # Else print the iterator value.\r\n print(itr)\r\n<\/pre>\n
1\r\n2\r\n3 - Fizz\r\n4\r\n5 - Buzz\r\n6 - Fizz\r\n7\r\n8<\/pre>\n
Method #2:Using For loop(User Input)<\/h3>\n
\n
# Give the number(upper limit) as user input using the int(input()) function \r\n# and store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\nprint(\"The output is :\")\r\n# Loop from 1 to the given number using the for loop.\r\nfor itr in range(1, gvn_numb+1):\r\n # Check if the iterator value is divisible by 3 and 5 using the modulus operator\r\n # and if conditional statement.\r\n if itr % 3 == 0 and itr % 5 == 0:\r\n # If it is true, then print \"FizzBuzz\".\r\n print(itr, \" - FizzBuzz\")\r\n # Again Check if the iterator value is divisible by 3 using the modulus operator\r\n # and elif conditional statement.\r\n elif itr % 3 == 0:\r\n # If it is true, then print \"Fizz\".\r\n print(itr, \" - Fizz\")\r\n # Check if the iterator value is divisible by 5 using the modulus operator\r\n # and elif conditional statement.\r\n elif itr % 5 == 0:\r\n # If it is true, then print \"Buzz\".\r\n print(itr, \" - Buzz\")\r\n else:\r\n # Else print the iterator value.\r\n print(itr)\r\n<\/pre>\n
Enter some random number = 15\r\nThe output is :\r\n1\r\n2\r\n3 - Fizz\r\n4\r\n5 - Buzz\r\n6 - Fizz\r\n7\r\n8\r\n9 - Fizz\r\n10 - Buzz\r\n11\r\n12 - Fizz\r\n13\r\n14\r\n15 - FizzBuzz<\/pre>\n