Let’s play a game of FizzBuzz! It’s quite the same with your childhood "PopCorn" game, but with a little bit of twist to align it with programming.
Are you ready?
Instructions:
Input a positive integer in one line. This will serve as the ending point of your loop.
Loop from 1 to the ending point (inclusive) and perform the following statements:
If the number is only divisible by 3, print "Fizz"
If the number is only divisible by 5, print "Buzz"
If the number is divisible by both 3 and 5, print "FizzBuzz"
If nothing is true in the previous conditions, skip the number
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(){
int positiveInteger;
//Input a positive integer in one line. This will serve as the ending point of your loop.
printf("Enter positive integer: ");
scanf("%d",&positiveInteger);
//Loop from 1 to the ending point (inclusive) and perform the following statements:
for(int i=1;i<=positiveInteger;i++){
//If the number is only divisible by 3, print "Fizz"
if(i%3==0){
printf("Fizz\n");
}
//If the number is only divisible by 5, print "Buzz"
if(i%5==0){
printf("Buzz\n");
}
//If the number is divisible by both 3 and 5, print "FizzBuzz"
if(i%3==0 && i%5==0){
printf("FizzBuzz\n");
}
//If nothing is true in the previous conditions, skip the number
}
getchar();
getchar();
return 0;
}
Comments
Leave a comment