C program to Print 1 and 0 in alternative rows

Write a C Program to Print 1 and 0 in alternative rows with example. For this Number Pattern, we are going to use For Loop and While Loop.

C program to Print 1 and 0 in alternative rows Example 1

This program allows the user to enter the number of rows and column values. Here, we are going to print 1’s and 0’s in alternative rows. I mean, 1’s in Odd rows, and 0’s in even rows.

First, we used the Nested for loop to iterate every row and column element. Next, we used If statement inside the Nested For Loop to check whether the row is Even or Odd

/* C program to Print Number Pattern 1, 0 at alternative rows */

#include<stdio.h>
 
int main()
{
    int i, j, rows, columns;
     
    printf(" \nPlease Enter the Number of Rows : ");
    scanf("%d", &rows);
    
    printf(" \nPlease Enter the Number of Columns : ");
    scanf("%d", &columns);
     
    for(i = 1; i <= rows; i++)
    {
    	for(j = 1; j <= columns; j++)
		{
			if(i % 2 != 0)
			{
				printf("1");
			}
			else
			{
				printf("0");
			}       	
        }
        printf("\n");
    }
    return 0;
}
C program to Print 1 and 0 in alternative rows 1

Program to Print 1 and 0 in alternative rows Example 2

This 1 and 0’s alternative rows C program is the same as the first example, but this time we are using a While loop (Just replacing For Loop with While Loop). I suggest you refer to While Loop to understand the loop Iteration.

/* C program to Print Number Pattern 1, 0 at alternative rows */

#include<stdio.h>
 
int main()
{
    int i, j, rows, columns;
    i = 1;
     
    printf(" \nPlease Enter the Number of Rows : ");
    scanf("%d", &rows);
    
    printf(" \nPlease Enter the Number of Columns : ");
    scanf("%d", &columns);
     
    while(i <= rows)
    {
    	j = 1;
    	while(j <= columns)
		{
			if(i % 2 != 0)
			{
				printf("1");
			}
			else
			{
				printf("0");
			}
			j++;       	
        }
        i++;
        printf("\n");
    }
    return 0;
}
Please Enter the Number of Rows : 7
 
Please Enter the Number of Columns : 10
1111111111
0000000000
1111111111
0000000000
1111111111
0000000000
1111111111

Program to Print 1 and 0 in alternative rows Example 3

This program is same as first example, but this time we are performing i % 2 directly inside the printf statement. By this, you can avoid using If statement.

/* C program to Print Number Pattern 1, 0 at alternative rows */

#include<stdio.h>
 
int main()
{
    int i, j, rows, columns;
     
    printf(" \nPlease Enter the Number of Rows : ");
    scanf("%d", &rows);
    
    printf(" \nPlease Enter the Number of Columns : ");
    scanf("%d", &columns);
     
    for(i = 1; i <= rows; i++)
    {
    	for(j = 1; j <= columns; j++)
		{
			printf("%d", i % 2);    	
        }
        printf("\n");
    }
    return 0;
}
Please Enter the Number of Rows : 12
 
Please Enter the Number of Columns : 15
111111111111111
000000000000000
111111111111111
000000000000000
111111111111111
000000000000000
111111111111111
000000000000000
111111111111111
000000000000000
111111111111111
000000000000000

Program to Print 1 and 0 in alternative rows Example 4

This C program for 1 and 0’s as alternative rows is same as above example but we changed the printing. I mean, this program will print 0’s in Odd rows, and 1’s in Even rows.

/* C program to Print Number Pattern 1, 0 at alternative rows */

#include<stdio.h>
 
int main()
{
    int i, j, rows, columns;
     
    printf(" \nPlease Enter the Number of Rows : ");
    scanf("%d", &rows);
    
    printf(" \nPlease Enter the Number of Columns : ");
    scanf("%d", &columns);
     
    for(i = 1; i <= rows; i++)
    {
    	for(j = 1; j <= columns; j++)
		{
			if(i % 2 != 0)
			{
				printf("0");
			}
			else
			{
				printf("1");
			}       	
        }
        printf("\n");
    }
    return 0;
}
Please Enter the Number of Rows : 8
 
Please Enter the Number of Columns : 12
000000000000
111111111111
000000000000
111111111111
000000000000
111111111111
000000000000
111111111111