Fizz Buzz Implementation in C

What is FizzBuzz program?

The program prints the numbers from 1 to n
where, for a given input number (n), for each integer i in the range from 1 to n

1. If i is a multiple of both 3 and 5 prints “FizzBuzz” instead of number
2. If i is a multiple of 3 but not 5 prints “Fizz” instead of number
3. If i is a multiple of 5 but not 3 prints “Buzz” instead of number
4. If i is a not multiple of 3 or 5 prints i value as usual.

Sample Input:

n= 15

Sample Output:

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz

Explanation:

The numbers 3, 6, 9, 12 are multiples of 3 so prints Fizz
The numbers 5,10 are multiples of 5 so prints Buzz.
15 is the multiple of both 3 and 5 so prints the FizzBuzz
None of the other values are not multiple of a 3 or 5 so prints i value.

Program in C:

include

int main()
{
int i, n ;
printf(“Enter the number :”);
scanf(“%d”, &n);
for (i=1; i<=n; i++)
{
// number divisible by 3 and 5 will always be divisible by 15, print ‘FizzBuzz’ in place of the number
if (i%15 == 0)
printf (“FizzBuzz\t”);

// number divisible by 3, print ‘Fizz’ in place of the number
else if ((i%3) == 0)
printf(“Fizz\t”);

// number divisible by 5, print ‘Buzz’ in place of the number
else if ((i%5) == 0)
printf(“Buzz\t”);

    else // print the number
        printf("%d\t", i);
}
return 0;

}

Test Case 1:

Enter the number :12
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz
Test Case 2:

Enter the number :65
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
Test Case 3:

Enter the number :269
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 101 Fizz 103 104 FizzBuzz 106 107 Fizz 109 Buzz Fizz 112 113 Fizz Buzz 116 Fizz 118 119 FizzBuzz 121 122 Fizz 124 Buzz Fizz 127 128 Fizz Buzz 131 Fizz 133 134 FizzBuzz 136 137 Fizz 139 Buzz Fizz 142 143 Fizz Buzz 146 Fizz 148 149 FizzBuzz 151 152 Fizz 154 Buzz Fizz 157 158 Fizz Buzz 161 Fizz 163 164 FizzBuzz 166 167 Fizz 169 Buzz Fizz 172 173 Fizz Buzz 176 Fizz 178 179 FizzBuzz 181 182 Fizz 184 Buzz Fizz 187 188 Fizz Buzz 191 Fizz 193 194 FizzBuzz 196 197 Fizz 199 Buzz Fizz 202 203 Fizz Buzz 206 Fizz 208 209 FizzBuzz 211 212 Fizz 214 Buzz Fizz 217 218 Fizz Buzz 221 Fizz 223 224 FizzBuzz 226 227 Fizz 229 Buzz Fizz 232 233 Fizz Buzz 236 Fizz 238 239 FizzBuzz 241 242 Fizz 244 Buzz Fizz 247 248 Fizz Buzz 251 Fizz 253 254 FizzBuzz 256 257 Fizz 259 Buzz Fizz 262 263 Fizz Buzz 266 Fizz 268 269
Test Case 4:

Enter the number :8
1 2 Fizz 4 Buzz Fizz 7 8
In the above test case 4, 3 is multiple of 3, so it prints Fizz and 5 is multiple of 5, so it prints Buzz. 6 is again multiple of 3, so it prints Fizz, 7 and 8 are not multiples of either 3 or 5, so the numbers are printed as usual.

Leave a Reply

Your email address will not be published. Required fields are marked *