Write a C++ Program to Print 1 and 0 Row Pattern using For Loop with an example. In this C++ example, we used the if-else statement inside the nested for loop to check for the even and odd number rows. If it is an odd row, print 1, and if it is an even row, then 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 Number Pattern-----\n";
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= columns; j++)
{
if(i % 2 != 0)
{
cout << "1";
}
else
{
cout << "0";
}
}
cout << "\n";
}
return 0;
}

C++ Program to Print 1 and 0 Row 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 Number Pattern-----\n";
i = 1;
while(i <= rows)
{
j = 1;
while(j <= columns)
{
if(i % 2 != 0)
{
cout << "1";
}
else
{
cout << "0";
}
j++;
}
cout << "\n";
i++;
}
return 0;
}

In this C++ 1 and 0 Row Pattern example, we are printing the result of i% 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 Number Pattern-----\n";
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= columns; j++)
{
cout << i % 2;
}
cout << "\n";
}
return 0;
}
