Thursday, October 9, 2025
HomeData Modelling & AIProgram to print binary right angle triangle

Program to print binary right angle triangle

Binary right angle triangle consists of only 0’s and 1’s in alternate positions. 
Examples : 
 

Input : 4
Output :
         0    
         1    0    
         0    1    0    
         1    0    1    0

Input : 3
Output : 
         0    
         1    0    
         0    1    0    

 

 

C++




// C program to print binary right angle
// triangle.
#include <stdio.h>
  
// function to print binary right angle
// triangle
void binaryRightAngleTriangle(int n) {
   
    // declare row and column
    int row, col; 
      
    for (row = 0; row < n; row++) 
    {
        for (col = 0;col <= row; col++) 
        {
            if (((row + col) % 2) == 0) 
                printf("0");
            else
                printf("1");
            printf("\t");
        }
        printf("\n");
    }   
}
  
// driver code
int main(void
{   
    // no. of rows to be printed
    int n = 4;
    binaryRightAngleTriangle(n);
    return 0;
}


Java




// Java program to print binary 
// right angle triangle
import java.io.*;
public class GFG
{
      
    // function to print binary right 
    // angle triangle
    static void binaryRightAngleTriangle(int n)
    {
      
        // declare row and column
        int row, col; 
          
        for (row = 0; row < n; row++) 
        {
            for (col = 0; col <= row; col++) 
            {
                if (((row + col) % 2) == 0
                    System.out.print("0");
                else
                    System.out.print("1");
                      
                System.out.print("\t");
            }
              
            System.out.print("\n");
        
    }
      
    // Driver code
    public static void main (String[] args)
    {
        // no. of rows to be printed
        int n = 4;
          
        binaryRightAngleTriangle(n);
    }
}
  
// This code is contributed 
// by Anant Agarwal.


Python3




# Python 3 program to print 
# binary right angle triangle.
  
# function to print binary 
# right angle triangle
def binaryRightAngleTriangle(n):
  
    # declare row and column
    for row in range(0, n): 
      
        for col in range(0, row + 1): 
          
            if (((row + col) % 2) == 0) :
                print("0", end = "")
            else:
                print("1", end = "")
            print("\t", end = "")
          
        print("")
      
# Driver Code
# no. of rows to be printed
n = 4
binaryRightAngleTriangle(n)
  
# This code is contributed
# by Smitha


C#




// C# program to print binary  
// right angle triangle 
using System; 
class GFG 
        
    // function to print binary right  
    // angle triangle 
    static void binaryRightAngleTriangle(int n) 
    
        
        // declare row and column 
        int row, col;  
            
        for (row = 0; row < n; row++)  
        
            for (col = 0; col <= row; col++)  
            
                if (((row + col) % 2) == 0)  
                    Console.Write("0"); 
                else 
                    Console.Write("1"); 
                        
                    Console.Write("\t"); 
            
                
                  Console.WriteLine(); 
        }  
    
        
    // Driver code 
    public static void Main () 
    
        // no. of rows to be printed 
        int n = 4; 
            
        binaryRightAngleTriangle(n); 
    
    
// This code is contributed  
// by vt_m .


PHP




<?php
// PHP program to print binary 
// right angle triangle.
  
// function to print binary 
// right angle triangle
function binaryRightAngleTriangle($n
{
    for ($row = 0; $row < $n; $row++) 
    {
        for ($col = 0;$col <= $row; $col++) 
        {
            if ((($row + $col) % 2) == 0) 
                printf("0");
            else
                printf("1");
            printf("\t");
        }
        printf("\n");
    
}
  
// Driver code
$n = 4;
binaryRightAngleTriangle($n);
  
// This code is contributed by mits 
?>


Javascript




<script>
      // JavaScript program to print binary right angle
      // triangle.
  
      // function to print binary right angle
      // triangle
      function binaryRightAngleTriangle(n) {
        // declare row and column
        var row, col;
  
        for (row = 0; row < n; row++) {
          for (col = 0; col <= row; col++) {
            if ((row + col) % 2 == 0) document.write("0");
            else document.write("1");
            document.write(" ");
          }
          document.write("<br>");
        }
      }
  
      // driver code
      // no. of rows to be printed
      var n = 4;
      binaryRightAngleTriangle(n);
        
      // This code is contributed by rdtank.
    </script>


Output

0    
1    0    
0    1    0    
1    0    1    0    

Time complexity: O(n*n)

Auxiliary space: O(1)

This article is contributed by Bishal Kumar Dubey. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32344 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6714 POSTS0 COMMENTS
Nicole Veronica
11877 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11940 POSTS0 COMMENTS
Shaida Kate Naidoo
6834 POSTS0 COMMENTS
Ted Musemwa
7094 POSTS0 COMMENTS
Thapelo Manthata
6789 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS