Print binary right angle triangle

Here given code implementation process.

//  C program for
//  Print binary right angle triangle
#include <stdio.h>

void printPattern(int height)
{
	// Display given height
	printf("\n Height : %d  \n", height);
	for (int i = 0; i < height; ++i)
	{
		for (int j = 0; j <= i; ++j)
		{
			// Print triangle value
			printf(" %d", (i + j) & 1);
		}
		printf("\n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test
	printPattern(5);
	printPattern(9);
	printPattern(7);
	printPattern(8);
	return 0;
}

Output

 Height : 5
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0

 Height : 9
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
 0 1 0 1 0 1 0 1 0

 Height : 7
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0

 Height : 8
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
/*
    Java Program
    Print binary right angle triangle
*/
public class Pattern
{
	public void printPattern(int height)
	{
		// Display given height
		System.out.print("\n Height : " + height + " \n");
		for (int i = 0; i < height; ++i)
		{
			for (int j = 0; j <= i; ++j)
			{
				// Print triangle value
				System.out.print(" " + ((i + j) & 1));
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(7);
		task.printPattern(8);
	}
}

Output

 Height : 5
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0

 Height : 9
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
 0 1 0 1 0 1 0 1 0

 Height : 7
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0

 Height : 8
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program
    Print binary right angle triangle
*/
class Pattern
{
	public: void printPattern(int height)
	{
		// Display given height
		cout << "\n Height : " << height << " \n";
		for (int i = 0; i < height; ++i)
		{
			for (int j = 0; j <= i; ++j)
			{
				// Print triangle value
				cout << " " << ((i + j) &1);
			}
			cout << "\n";
		}
	}
};
int main()
{
	Pattern *task = new Pattern();
	// Test
	task->printPattern(5);
	task->printPattern(9);
	task->printPattern(7);
	task->printPattern(8);
	return 0;
}

Output

 Height : 5
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0

 Height : 9
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
 0 1 0 1 0 1 0 1 0

 Height : 7
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0

 Height : 8
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
// Include namespace system
using System;
/*
    Csharp Program
    Print binary right angle triangle
*/
public class Pattern
{
	public void printPattern(int height)
	{
		// Display given height
		Console.Write("\n Height : " + height + " \n");
		for (int i = 0; i < height; ++i)
		{
			for (int j = 0; j <= i; ++j)
			{
				// Print triangle value
				Console.Write(" " + ((i + j) & 1));
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(7);
		task.printPattern(8);
	}
}

Output

 Height : 5
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0

 Height : 9
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
 0 1 0 1 0 1 0 1 0

 Height : 7
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0

 Height : 8
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
package main
import "fmt"
/*
    Go Program
    Print binary right angle triangle
*/
type Pattern struct {}
func getPattern() * Pattern {
	var me *Pattern = &Pattern {}
	return me
}
func(this Pattern) printPattern(height int) {
	// Display given height
	fmt.Print("\n Height : ", height, " \n")
	for i := 0 ; i < height ; i++ {
		for j := 0 ; j <= i ; j++ {
			// Print triangle value
			fmt.Print(" ", ((i + j) & 1))
		}
		fmt.Print("\n")
	}
}
func main() {
	var task * Pattern = getPattern()
	// Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(7)
	task.printPattern(8)
}

Output

 Height : 5
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0

 Height : 9
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
 0 1 0 1 0 1 0 1 0

 Height : 7
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0

 Height : 8
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
<?php
/*
    Php Program
    Print binary right angle triangle
*/
class Pattern
{
	public	function printPattern($height)
	{
		// Display given height
		echo("\n Height : ".$height." \n");
		for ($i = 0; $i < $height; ++$i)
		{
			for ($j = 0; $j <= $i; ++$j)
			{
				// Print triangle value
				echo(" ".(($i + $j) & 1));
			}
			echo("\n");
		}
	}
}

function main()
{
	$task = new Pattern();
	// Test
	$task->printPattern(5);
	$task->printPattern(9);
	$task->printPattern(7);
	$task->printPattern(8);
}
main();

Output

 Height : 5
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0

 Height : 9
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
 0 1 0 1 0 1 0 1 0

 Height : 7
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0

 Height : 8
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
/*
    Node JS Program
    Print binary right angle triangle
*/
class Pattern
{
	printPattern(height)
	{
		// Display given height
		process.stdout.write("\n Height : " + height + " \n");
		for (var i = 0; i < height; ++i)
		{
			for (var j = 0; j <= i; ++j)
			{
				// Print triangle value
				process.stdout.write(" " + ((i + j) & 1));
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var task = new Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(7);
	task.printPattern(8);
}
main();

Output

 Height : 5
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0

 Height : 9
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
 0 1 0 1 0 1 0 1 0

 Height : 7
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0

 Height : 8
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
#    Python 3 Program
#    Print binary right angle triangle
class Pattern :
	def printPattern(self, height) :
		#  Display given height
		print("\n Height : ", height ," ")
		i = 0
		while (i < height) :
			j = 0
			while (j <= i) :
				#  Print triangle value
				print(" ", ((i + j) & 1), end = "")
				j += 1
			
			print(end = "\n")
			i += 1
		
	

def main() :
	task = Pattern()
	#  Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(7)
	task.printPattern(8)

if __name__ == "__main__": main()

Output

 Height :  5
  0
  1  0
  0  1  0
  1  0  1  0
  0  1  0  1  0

 Height :  9
  0
  1  0
  0  1  0
  1  0  1  0
  0  1  0  1  0
  1  0  1  0  1  0
  0  1  0  1  0  1  0
  1  0  1  0  1  0  1  0
  0  1  0  1  0  1  0  1  0

 Height :  7
  0
  1  0
  0  1  0
  1  0  1  0
  0  1  0  1  0
  1  0  1  0  1  0
  0  1  0  1  0  1  0

 Height :  8
  0
  1  0
  0  1  0
  1  0  1  0
  0  1  0  1  0
  1  0  1  0  1  0
  0  1  0  1  0  1  0
  1  0  1  0  1  0  1  0
#    Ruby Program
#    Print binary right angle triangle
class Pattern 
	def printPattern(height) 
		#  Display given height
		print("\n Height : ", height ," \n")
		i = 0
		while (i < height) 
			j = 0
			while (j <= i) 
				#  Print triangle value
				print(" ", ((i + j) & 1))
				j += 1
			end

			print("\n")
			i += 1
		end

	end

end

def main() 
	task = Pattern.new()
	#  Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(7)
	task.printPattern(8)
end

main()

Output

 Height : 5 
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0

 Height : 9 
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
 0 1 0 1 0 1 0 1 0

 Height : 7 
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0

 Height : 8 
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
/*
    Scala Program
    Print binary right angle triangle
*/
class Pattern()
{
	def printPattern(height: Int): Unit = {
		// Display given height
		print("\n Height : " + height + " \n");
		var i: Int = 0;
		while (i < height)
		{
			var j: Int = 0;
			while (j <= i)
			{
				// Print triangle value
				print(" " + ((i + j) & 1));
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pattern = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(7);
		task.printPattern(8);
	}
}

Output

 Height : 5
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0

 Height : 9
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
 0 1 0 1 0 1 0 1 0

 Height : 7
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0

 Height : 8
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
/*
    Swift 4 Program
    Print binary right angle triangle
*/
class Pattern
{
	func printPattern(_ height: Int)
	{
		// Display given height
		print("\n Height : ", height ," ");
		var i: Int = 0;
		while (i < height)
		{
			var j: Int = 0;
			while (j <= i)
			{
				// Print triangle value
				print(" ", ((i + j) & 1), terminator: "");
				j += 1;
			}
			print(terminator: "\n");
			i += 1;
		}
	}
}
func main()
{
	let task: Pattern = Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(7);
	task.printPattern(8);
}
main();

Output

 Height :  5
  0
  1  0
  0  1  0
  1  0  1  0
  0  1  0  1  0

 Height :  9
  0
  1  0
  0  1  0
  1  0  1  0
  0  1  0  1  0
  1  0  1  0  1  0
  0  1  0  1  0  1  0
  1  0  1  0  1  0  1  0
  0  1  0  1  0  1  0  1  0

 Height :  7
  0
  1  0
  0  1  0
  1  0  1  0
  0  1  0  1  0
  1  0  1  0  1  0
  0  1  0  1  0  1  0

 Height :  8
  0
  1  0
  0  1  0
  1  0  1  0
  0  1  0  1  0
  1  0  1  0  1  0
  0  1  0  1  0  1  0
  1  0  1  0  1  0  1  0
/*
    Kotlin Program
    Print binary right angle triangle
*/
class Pattern
{
	fun printPattern(height: Int): Unit
	{
		// Display given height
		print("\n Height : " + height + " \n");
		var i: Int = 0;
		while (i < height)
		{
			var j: Int = 0;
			while (j <= i)
			{
				// Print triangle value
				print(" " + ((i + j) and 1));
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Pattern = Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(7);
	task.printPattern(8);
}

Output

 Height : 5
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0

 Height : 9
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0
 0 1 0 1 0 1 0 1 0

 Height : 7
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0

 Height : 8
 0
 1 0
 0 1 0
 1 0 1 0
 0 1 0 1 0
 1 0 1 0 1 0
 0 1 0 1 0 1 0
 1 0 1 0 1 0 1 0


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved