Write a C++ Program to Print 1 and 0 Column Pattern using For Loop with an example. In this C++ column pattern example, we used an if-else statement inside the nested for loop to check whether the column number is even or odd. If it is an odd column, print 0, and if it is an even column, print 1.
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns;
cout << "\nPlease Enter the Number of Rows = ";
cin >> rows;
cout << "\nPlease Enter the Number of Columns = ";
cin >> columns;
cout << "\n---1 and 0 Column Pattern-----\n";
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= columns; j++)
{
if(j % 2 != 0)
{
cout << "0";
}
else
{
cout << "1";
}
}
cout << "\n";
}
return 0;
}
C++ Program to Print 0 and 1 Column Pattern using a While Loop
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns;
cout << "\nPlease Enter the Number of Rows = ";
cin >> rows;
cout << "\nPlease Enter the Number of Columns = ";
cin >> columns;
cout << "\n---1 and 0 Column Pattern-----\n";
i = 1;
while(i <= rows)
{
j = 1;
while(j <= columns)
{
if(j % 2 != 0)
{
cout << "0";
}
else
{
cout << "1";
}
j++;
}
cout << "\n";
i++;
}
return 0;
}
In this C++ code to Print 1 and 0 Column Pattern, If it is an odd column, print 1, and if it is an even column, print 0.
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns;
cout << "\nPlease Enter the Number of Rows = ";
cin >> rows;
cout << "\nPlease Enter the Number of Columns = ";
cin >> columns;
cout << "\n---1 and 0 Column Pattern-----\n";
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= columns; j++)
{
if(j % 2 != 0)
{
cout << "1";
}
else
{
cout << "0";
}
}
cout << "\n";
}
return 0;
}
In this C++ 1 and 0 column Pattern example, we are printing the result of j % 2 results directly inside the nested for loop.
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns;
cout << "\nPlease Enter the Number of Rows = ";
cin >> rows;
cout << "\nPlease Enter the Number of Columns = ";
cin >> columns;
cout << "\n---1 and 0 Column Pattern-----\n";
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= columns; j++)
{
cout << j % 2;
}
cout << "\n";
}
return 0;
}