De FizzBuzz-uitdaging voltooien in 5 programmeertalen

De FizzBuzz-uitdaging voltooien in 5 programmeertalen

De FizzBuzz-uitdaging is een klassieke uitdaging die wordt gebruikt als een screeningapparaat voor interviews voor computerprogrammeurs. Het is een heel eenvoudige programmeertaak, maar het wordt gebruikt om te bepalen of de sollicitant daadwerkelijk code kan schrijven.





Klinkt leuk en spannend? Laten we beginnen. In dit artikel leer je hoe je de FizzBuzz-uitdaging oplost met implementaties in 5 programmeertalen.





Probleemstelling

U moet een programma schrijven dat de getallen van 1 tot 100 afdrukt, zodat:





  1. Als het getal een veelvoud van 3 is, moet je 'Fizz' afdrukken in plaats van dat getal.
  2. Als het getal een veelvoud van 5 is, moet je 'Buzz' afdrukken in plaats van dat getal.
  3. Als het getal een veelvoud is van zowel 3 als 5, moet je 'FizzBuzz' afdrukken in plaats van dat getal.

Probeer een oplossing te bedenken om deze uitdaging op te lossen met behulp van lussen en voorwaardelijke instructies voordat u naar de oplossing gaat.

Aanpak om de FizzBuzz-uitdaging op te lossen

U moet de onderstaande aanpak volgen om deze uitdaging op te lossen:



we hebben problemen met het spelen van deze titel
  1. Voer een lus uit van 1 tot 100.
  2. Getallen die deelbaar zijn door 3 en 5 zijn altijd deelbaar door 15. Controleer daarom de voorwaarde als een getal deelbaar is door 15. Als het getal deelbaar is door 15, print dan 'FizzBuzz'.
  3. Controleer de voorwaarde als een getal deelbaar is door 3. Als het getal deelbaar is door 3, print je 'Fizz'.
  4. Controleer de voorwaarde als een getal deelbaar is door 5. Als het getal deelbaar is door 5, print je 'Buzz'.

Opmerking : U kunt controleren of een getal deelbaar is door een ander getal met behulp van de modulo-operator (%). Bijvoorbeeld: 25 % 5 == 0, dus 25 is deelbaar door 5.

Pseudocode voor de FizzBuzz-uitdaging

Hieronder staat de pseudocode voor de FizzBuzz-uitdaging:





for number from 1 to 100:
if (number is divisible by 3 and 5) then:
print('FizzBuzz')
if (number is divisible by 3) then:
print('Fizz')
if (number is divisible by 5) then:
print('Buzz')

Verwant: Wat is coderen en hoe werkt het?

C++-programma om de FizzBuzz-uitdaging op te lossen

Hieronder staat het C++-programma om de FizzBuzz-uitdaging op te lossen:





// C++ program to implement the FizzBuzz problem
#include
using namespace std;
int main()
{
for (int i=1; i<=100; i++)
{
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, 'FizzBuzz' is printed in place of that number
if (i%15 == 0)
{
cout << 'FizzBuzz' << ' ';
}
// 'Fizz' is printed in place of numbers
// that are divisible by 3
else if ((i%3) == 0)
{
cout << 'Fizz' << ' ';
}
// 'Buzz' is printed in place of numbers
// that are divisible by 5
else if ((i%5) == 0)
{
cout << 'Buzz' << ' ';
}
// If none of the above conditions are satisfied,
// the number is printed
else
{
cout << i << ' ';
}
}
return 0;
}

Uitgang:

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz

Verwant: Hoe u C++-programmering leert: de beste sites om aan de slag te gaan

Python-programma om de FizzBuzz-uitdaging op te lossen

Hieronder staat het Python-programma om de FizzBuzz-uitdaging op te lossen:

# Python program to implement the FizzBuzz problem
for i in range(1, 101):
# Numbers that are divisible by 3 and 5
# are always divisible by 15
# Therefore, 'FizzBuzz' is printed in place of that number
if (i%15 == 0):
print('FizzBuzz', end=' ')
# 'Fizz' is printed in place of numbers
# that are divisible by 3
elif (i%3 == 0):
print('Fizz', end=' ')
# 'Buzz' is printed in place of numbers
# that are divisible by 5
elif(i%5 == 0):
print('Buzz', end=' ')
# If none of the above conditions are satisfied,
# the number is printed
else:
print(i, end=' ')

Uitgang:

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz

Verwant: Aan de slag met Python met behulp van een 'Hello World'-script

JavaScript-programma om de FizzBuzz-uitdaging op te lossen

Hieronder staat het JavaScript-programma om de FizzBuzz-uitdaging op te lossen:

// JavaScript program to implement the FizzBuzz problem
for (let i=1; i<=100; i++) {
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, 'FizzBuzz' is printed in place of that number
if (i%15 == 0) {
document.write('FizzBuzz' + ' ');
}
// 'Fizz' is printed in place of numbers
// that are divisible by 3
else if ((i%3) == 0) {
document.write('Fizz' + ' ');
}
// 'Buzz' is printed in place of numbers
// that are divisible by 5
else if ((i%5) == 0) {
document.write('Buzz' + ' ');
}
// If none of the above conditions are satisfied,
// the number is printed
else {
document.write(i + ' ');
}
}

Uitgang:

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz

Verwant: De beste beginnersprojecten voor nieuwe programmeurs

Java-programma om de FizzBuzz-uitdaging op te lossen

Hieronder staat het Java-programma om de FizzBuzz-uitdaging op te lossen:

// Java program to implement the FizzBuzz problem
public class Main
{
public static void main(String args[])
{
for (int i=1; i<=100; i++)
{
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, 'FizzBuzz' is printed in place of that number
if (i%15==0)
{
System.out.print('FizzBuzz'+' ');
}
// 'Fizz' is printed in place of numbers
// that are divisible by 3
else if (i%3==0)
{
System.out.print('Fizz'+' ');
}
// 'Buzz' is printed in place of numbers
// that are divisible by 5
else if (i%5==0)
{
System.out.print('Buzz'+' ');
}
// If none of the above conditions are satisfied,
// the number is printed
else
{
System.out.print(i+' ');
}
}
}
}

Uitgang:

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz

C-programma om de FizzBuzz-uitdaging op te lossen

Hieronder staat het C-programma om de FizzBuzz-uitdaging op te lossen:

// C program to implement the FizzBuzz problem
#include
int main()
{
for (int i=1; i<=100; i++)
{
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, 'FizzBuzz' is printed in place of that number
if (i%15 == 0)
{
printf('FizzBuzz ');
}
// 'Fizz' is printed in place of numbers
// that are divisible by 3
else if ((i%3) == 0)
{
printf('Fizz ');
}
// 'Buzz' is printed in place of numbers
// that are divisible by 5
else if ((i%5) == 0)
{
printf('Buzz ');
}
// If none of the above conditions are satisfied,
// the number is printed
else
{
printf('%d ', i);
}
}
return 0;
}

Uitgang:

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz

Begin uw codeerreis met een 'Hallo, wereld!' Programma

De 'Hallo wereld!' programma is de eerste stap voor programmeurs om kennis te maken met een nieuwe programmeertaal. Het wordt beschouwd als een van de eenvoudigste programma's die in bijna alle talen mogelijk is.

Als je een beginner bent in de programmeerwereld en verschillende talen verkent, is de 'Hello, World!' programma is de beste keuze om met een nieuwe programmeertaal aan de slag te gaan.

Deel Deel Tweeten E-mail 'Hallo wereld!' afdrukken in de 20 meest populaire programmeertalen

Groet de wereld en ontdek de meest gevraagde programmeertalen.

Lees volgende
Gerelateerde onderwerpen
  • Programmeren
  • JavaScript
  • Java
  • Python
  • C Programmeren
Over de auteur Yuvraj Chandra(60 artikelen gepubliceerd)

Yuvraj is een student Computerwetenschappen aan de Universiteit van Delhi, India. Hij is gepassioneerd door Full Stack Web Development. Als hij niet aan het schrijven is, onderzoekt hij de diepte van verschillende technologieën.

hoe hotmail-account 2018 te verwijderen
Meer van Yuvraj Chandra

Abonneer op onze nieuwsbrief

Word lid van onze nieuwsbrief voor technische tips, recensies, gratis e-boeken en exclusieve deals!

Klik hier om je te abonneren