1
10
101
1010
The number of elements in each row equals the row number + 1.
The first column (col = 0) should always be 1, and then it alternates.
If col is even, print 1; if odd, print 0.
Java |
public class Main { public static void main(String[] args) { int total_rows = 4; // Number of rows // Outer loop for rows (starting from 0) for (int row = 0; row < total_rows; row++) { // Inner loop for columns (starting from 0) for (int col = 0; col <= row; col++) { // Print '1' for even index, '0' for odd index System.out.print(col % 2 == 0 ? "1" : "0"); } System.out.println(); // Move to the next line } } } |
C++ |
#include <iostream> using namespace std; int main() { int total_rows = 4; // Number of rows // Outer loop for rows (starting from 0) for (int row = 0; row < total_rows; row++) { // Inner loop for columns (starting from 0) for (int col = 0; col <= row; col++) { // Print '1' for even index, '0' for odd index cout << (col % 2 == 0 ? "1" : "0"); } cout << endl; // Move to the next line } return 0; } |
Python |
total_rows = 4 # Number of rows # Outer loop for rows (starting from 0) for row in range(total_rows): # Inner loop for columns (starting from 0) for col in range(row + 1): print("1" if col % 2 == 0 else "0", end="") # Print '1' for even, '0' for odd print() # Move to the next line |