LearnFrenzy
Sign in Take a free test

Control Instructions · Predict the Output · Question 6

FizzBuzz with an else-if ladder – output Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    for (int x = 9; x <= 15; x++) {
        if (x % 15 == 0)
            printf("FizzBuzz ");
        else if (x % 3 == 0)
            printf("Fizz ");
        else if (x % 5 == 0)
            printf("Buzz ");
        else
            printf("%d ", x);
    }
    printf("\n");
    return 0;
}
  • A Fizz Buzz 11 Fizz 13 14 FizzBuzz Correct
  • B Fizz Buzz 11 Fizz 13 14 Fizz
  • C 9 10 11 12 13 14 15
  • D Fizz Buzz 11 Fizz 13 14 Buzz
Correct Answer: Option A

The ladder stops at the first true test, so the most specific check (divisible by 15) must come first; otherwise 15 would print just "Fizz".

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links