LearnFrenzy
Sign in Take a free test

C Loops · Predict the Output · Question 12

Nested loop printing a 0/1 pattern – output Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    for (int r = 1; r <= 4; r++) {
        for (int c = 1; c <= r; c++)
            printf("%d", c % 2);
        printf("\n");
    }
    return 0;
}
  • A 1 10 101 1010 Correct
  • B 1 01 101 0101
  • C 1 11 111 1111
  • D 0 01 010 0101
Correct Answer: Option A

Row r prints r characters, each c % 2 for c = 1 to r: 1, 0, 1, 0 … Every row starts with 1 because c starts at 1.

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