C program to Print 1 and 0 in alternative columns

How to write a C program to Print 1 and 0 in alternative columns 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 columns 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 at alternate columns. I mean, 1’s in Odd columns, and 0’s in even columns.

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

/* C program to Print Number Pattern 1, 0 at Alternative Columns */

#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(j % 2 == 0)
			{
				printf("0");
			}
			else
			{
				printf("1");
			}       	
        }
        printf("\n");
    }
    return 0;
}
Please Enter the Number of Rows : 5
 
Please Enter the Number of Columns : 8
10101010
10101010
10101010
10101010
10101010

Program to Print 1 and 0 in alternative columns Example 2

This 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 Columns */

#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(j % 2 == 0)
			{
				printf("0");
			}
			else
			{
				printf("1");
			}
			j++;       	
        }
        i++;
        printf("\n");
    }
    return 0;
}
Please Enter the Number of Rows : 8
 
Please Enter the Number of Columns : 14
10101010101010
10101010101010
10101010101010
10101010101010
10101010101010
10101010101010
10101010101010
10101010101010

Program to Print 1 and 0 in alternative columns Example 3

This program is same as first example, but this time we are using 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 Columns */

#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", j % 2);    	
        }
        printf("\n");
    }
    return 0;
}
C program to Print 1 and 0 in alternative columns 3

Program to Print 1 and 0 in alternative columns Example 4

This C program for 1 and 0’s as alternate columns 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 Columns */

#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(j % 2 == 0)
			{
				printf("1");
			}
			else
			{
				printf("0");
			}       	
        }
        printf("\n");
    }
    return 0;
}
Please Enter the Number of Rows : 10
 
Please Enter the Number of Columns : 25
0101010101010101010101010
0101010101010101010101010
0101010101010101010101010
0101010101010101010101010
0101010101010101010101010
0101010101010101010101010
0101010101010101010101010
0101010101010101010101010
0101010101010101010101010
0101010101010101010101010