打印二进制直角三角形的程序

二元直角三角形仅由交替位置的0和1组成。 例如:

null
Input : 4Output :         0             1    0             0    1    0             1    0    1    0Input : 3Output :          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 ( " " );
}
printf ( "" );
}
}
// 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
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( " " );
}
System.out.print( "" );
}
}
// 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 ( " " , 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( " " );
}
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( " " );
}
printf( "" );
}
}
// 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>


输出:

0    1    0    0    1    0    1    0    1    0        

本文由 比沙尔·库马尔·杜比 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享