Program to print binary right angle triangle
Last Updated :
16 Feb, 2023
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++
#include <stdio.h>
void binaryRightAngleTriangle( int n) {
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" );
}
}
int main( void )
{
int n = 4;
binaryRightAngleTriangle(n);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static void binaryRightAngleTriangle( int n)
{
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" );
}
}
public static void main (String[] args)
{
int n = 4 ;
binaryRightAngleTriangle(n);
}
}
|
Python3
def binaryRightAngleTriangle(n):
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 ("")
n = 4
binaryRightAngleTriangle(n)
|
C#
using System;
class GFG
{
static void binaryRightAngleTriangle( int n)
{
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();
}
}
public static void Main ()
{
int n = 4;
binaryRightAngleTriangle(n);
}
}
|
PHP
<?php
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" );
}
}
$n = 4;
binaryRightAngleTriangle( $n );
?>
|
Javascript
<script>
function binaryRightAngleTriangle(n) {
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>" );
}
}
var n = 4;
binaryRightAngleTriangle(n);
</script>
|
Output
0
1 0
0 1 0
1 0 1 0
Time complexity: O(n*n)
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...