write c program to print the following pattern
01
0101
010101
01010101
0101010101
010101010101
Answers
Answered by
2
Answer:
This is the required C program for the question.
#include <stdio.h>
int main() {
int n,i,j;
printf("Enter number of rows - ");
scanf("%d",&n);
for(i=1;i<=n;i++) {
for(j=0;j<2*i;j++)
printf("%d",j%2);
printf("\n");
}
return 0;
}
Explanation:
- At first, we will ask the user to ask the number of rows, say n. Then, we will iterate a loop n times i.e., from i = 1 to n. (so as to print first n rows of the pattern).
- Then, to display the columns, we will create another loop. The loop iterates 2*i times where i is the row number (as each row contain 2n number of columns). So, here, the loop iterates from j = 1 to 2*i or we can write j = 0 to 2*i - 1.
- Then, inside the loop, we will display the value of j. j=0 initially. So, )%2=0, then j becomes 1. So, 1%2=1. In this way, we can solve this question.
See the attachment for output.
Attachments:
Similar questions