OUTPUT (after running):
Enter the number of rows:
6
Q8.
Write a C program to draw the following triangle:
1
01
101
0101
10101
010101
1010101
[Hint:
Let us write its algorithm first:
1. Read number of rows,
row.
2. Initialize i=1.
3. Repeat through step 8 while i is less than or equal to
row.
4. Initialize j=0.
5. Repeat through step 7 while is j less than i.
6. Print (i–j) % 2.
7. j=j+1
8. Move to the next line.
9. Stop and exit.
The program is as follows:
void main( )
{
int row, i, j;
printf(“Enter the number of rows:”);
scanf(“%d”, &row);
printf(“\n”);
for (i=1; i |
{
for (j=0; j {
printf(“%d”, (i-j) % 2);
}
printf(“\n”);
}
}
}
Do'stlaringiz bilan baham: |