C++ Program to Print Right Triangle of 1 and 0

Write a C++ program to print the right angled triangle of 1 and 0’s using for loop.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows;
     
    cout << "Enter Right Triangle of 1 & 0 Pattern Row = ";
    cin >> rows;

    cout << "Right Triangle of 1 and 0 Numbers Pattern\n"; 

    for(i = 1; i <= rows; i++)
    {
    	for(j = 1; j <= i; j++)
		{
            if(j % 2 == 0) {
                cout << 0 << " ";
            }
            else {
                cout << 1 << " ";
            } 
        }
        cout << "\n";
    }		
 	return 0;
}
C++ Program to Print Right Triangle of 1 and 0

This C++ example prints the right angled triangle with 1 and 0’s as alternative columns using a while loop.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows;
     
    cout << "Enter Right Triangle of 1 & 0 Pattern Row = ";
    cin >> rows;

    cout << "Right Triangle of 1 and 0 Numbers Pattern\n"; 

    i = 1;
    while( i <= rows)
    {
        j = 1;
    	while( j <= i)
		{
            if(j % 2 == 0) {
                cout << 0 << " ";
            }
            else {
                cout << 1 << " ";
            } 
             j++;
        }
        cout << "\n";
        i++;
    }		
 	return 0;
}
Enter Right Triangle of 1 & 0 Pattern Row = 15
Right Triangle of 1 and 0 Numbers Pattern
1 
1 0 
1 0 1 
1 0 1 0 
1 0 1 0 1 
1 0 1 0 1 0 
1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 
1 0 1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 1 0 
1 0 1 0 1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 1 0 1 0 
1 0 1 0 1 0 1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 1 0 1 0 1 0 
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1