Print modified binary triangle pattern

Here given code implementation process.

//  C program for
//  Print modified binary triangle pattern
#include <stdio.h>

void printTriangle(int height)
{
	printf("\nGiven height : %d \n", height);
  
	for (int i = 1; i <= height; ++i)
	{
		for (int j = 1; j <= i; ++j)
		{
			if (j == 1 || j == i)
			{
              	// Corner element is 1
				printf("1");
			}
			else
			{
              	// Intermediate element is 0
				printf("0");
			}
		}
		printf("\n");
	}
}
int main(int argc, char const *argv[])
{
	// Test
	printTriangle(7);
	printTriangle(12);
	return 0;
}

Output

Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
/*
    Java Program
    Print modified binary triangle pattern
*/
public class Pattern
{
	public void printTriangle(int height)
	{
		System.out.println("\nGiven height : " + height);
		// Outer loop control the row operation
		for (int i = 1; i <= height; ++i)
		{
			// Inner loop control the column operation
			for (int j = 1; j <= i; ++j)
			{
				if (j == 1 || j == i)
				{
					// Corner element is 1
					System.out.print("1");
				}
				else
				{
					// Intermediate element is 0
					System.out.print("0");
				}
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printTriangle(7);
		task.printTriangle(12);
	}
}

Output

Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Print modified binary triangle pattern
*/
class Pattern
{
	public: void printTriangle(int height)
	{
		cout << "\nGiven height : " << height << endl;
		// Outer loop control the row operation
		for (int i = 1; i <= height; ++i)
		{
			// Inner loop control the column operation
			for (int j = 1; j <= i; ++j)
			{
				if (j == 1 || j == i)
				{
					// Corner element is 1
					cout << "1";
				}
				else
				{
					// Intermediate element is 0
					cout << "0";
				}
			}
			cout << "\n";
		}
	}
};
int main()
{
	Pattern *task = new Pattern();
	// Test
	task->printTriangle(7);
	task->printTriangle(12);
	return 0;
}

Output

Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
// Include namespace system
using System;
/*
    Csharp Program
    Print modified binary triangle pattern
*/
public class Pattern
{
	public void printTriangle(int height)
	{
		Console.WriteLine("\nGiven height : " + height);
		// Outer loop control the row operation
		for (int i = 1; i <= height; ++i)
		{
			// Inner loop control the column operation
			for (int j = 1; j <= i; ++j)
			{
				if (j == 1 || j == i)
				{
					// Corner element is 1
					Console.Write("1");
				}
				else
				{
					// Intermediate element is 0
					Console.Write("0");
				}
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printTriangle(7);
		task.printTriangle(12);
	}
}

Output

Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
package main
import "fmt"
/*
    Go Program
    Print modified binary triangle pattern
*/
type Pattern struct {}
func getPattern() * Pattern {
	var me *Pattern = &Pattern {}
	return me
}
func(this Pattern) printTriangle(height int) {
	fmt.Println("\nGiven height : ", height)
	// Outer loop control the row operation
	for i := 1 ; i <= height ; i++ {
		// Inner loop control the column operation
		for j := 1 ; j <= i ; j++ {
			if j == 1 || j == i {
				// Corner element is 1
				fmt.Print("1")
			} else {
				// Intermediate element is 0
				fmt.Print("0")
			}
		}
		fmt.Print("\n")
	}
}
func main() {
	var task * Pattern = getPattern()
	// Test
	task.printTriangle(7)
	task.printTriangle(12)
}

Output

Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
<?php
/*
    Php Program
    Print modified binary triangle pattern
*/
class Pattern
{
	public	function printTriangle($height)
	{
		echo("\nGiven height : ".$height."\n");
		// Outer loop control the row operation
		for ($i = 1; $i <= $height; ++$i)
		{
			// Inner loop control the column operation
			for ($j = 1; $j <= $i; ++$j)
			{
				if ($j == 1 || $j == $i)
				{
					// Corner element is 1
					echo("1");
				}
				else
				{
					// Intermediate element is 0
					echo("0");
				}
			}
			echo("\n");
		}
	}
}

function main()
{
	$task = new Pattern();
	// Test
	$task->printTriangle(7);
	$task->printTriangle(12);
}
main();

Output

Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
/*
    Node JS Program
    Print modified binary triangle pattern
*/
class Pattern
{
	printTriangle(height)
	{
		console.log("\nGiven height : " + height);
		// Outer loop control the row operation
		for (var i = 1; i <= height; ++i)
		{
			// Inner loop control the column operation
			for (var j = 1; j <= i; ++j)
			{
				if (j == 1 || j == i)
				{
					// Corner element is 1
					process.stdout.write("1");
				}
				else
				{
					// Intermediate element is 0
					process.stdout.write("0");
				}
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var task = new Pattern();
	// Test
	task.printTriangle(7);
	task.printTriangle(12);
}
main();

Output

Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
#    Python 3 Program
#    Print modified binary triangle pattern
class Pattern :
	def printTriangle(self, height) :
		print("\nGiven height : ", height)
		i = 1
		#  Outer loop control the row operation
		while (i <= height) :
			j = 1
			#  Inner loop control the column operation
			while (j <= i) :
				if (j == 1 or j == i) :
					#  Corner element is 1
					print("1", end = "")
				else :
					#  Intermediate element is 0
					print("0", end = "")
				
				j += 1
			
			print(end = "\n")
			i += 1
		
	

def main() :
	task = Pattern()
	#  Test
	task.printTriangle(7)
	task.printTriangle(12)

if __name__ == "__main__": main()

Output

Given height :  7
1
11
101
1001
10001
100001
1000001

Given height :  12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
#    Ruby Program
#    Print modified binary triangle pattern
class Pattern 
	def printTriangle(height) 
		print("\nGiven height : ", height, "\n")
		i = 1
		#  Outer loop control the row operation
		while (i <= height) 
			j = 1
			#  Inner loop control the column operation
			while (j <= i) 
				if (j == 1 || j == i) 
					#  Corner element is 1
					print("1")
				else
 
					#  Intermediate element is 0
					print("0")
				end

				j += 1
			end

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

	end

end

def main() 
	task = Pattern.new()
	#  Test
	task.printTriangle(7)
	task.printTriangle(12)
end

main()

Output

Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
/*
    Scala Program
    Print modified binary triangle pattern
*/
class Pattern()
{
	def printTriangle(height: Int): Unit = {
		println("\nGiven height : " + height);
		var i: Int = 1;
		// Outer loop control the row operation
		while (i <= height)
		{
			var j: Int = 1;
			// Inner loop control the column operation
			while (j <= i)
			{
				if (j == 1 || j == i)
				{
					// Corner element is 1
					print("1");
				}
				else
				{
					// Intermediate element is 0
					print("0");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pattern = new Pattern();
		// Test
		task.printTriangle(7);
		task.printTriangle(12);
	}
}

Output

Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
/*
    Swift 4 Program
    Print modified binary triangle pattern
*/
class Pattern
{
	func printTriangle(_ height: Int)
	{
		print("\nGiven height : ", height);
		var i: Int = 1;
		// Outer loop control the row operation
		while (i <= height)
		{
			var j: Int = 1;
			// Inner loop control the column operation
			while (j <= i)
			{
				if (j == 1 || j == i)
				{
					// Corner element is 1
					print("1", terminator: "");
				}
				else
				{
					// Intermediate element is 0
					print("0", terminator: "");
				}
				j += 1;
			}
			print(terminator: "\n");
			i += 1;
		}
	}
}
func main()
{
	let task: Pattern = Pattern();
	// Test
	task.printTriangle(7);
	task.printTriangle(12);
}
main();

Output

Given height :  7
1
11
101
1001
10001
100001
1000001

Given height :  12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
/*
    Kotlin Program
    Print modified binary triangle pattern
*/
class Pattern
{
	fun printTriangle(height: Int): Unit
	{
		println("\nGiven height : " + height);
		var i: Int = 1;
		// Outer loop control the row operation
		while (i <= height)
		{
			var j: Int = 1;
			// Inner loop control the column operation
			while (j <= i)
			{
				if (j == 1 || j == i)
				{
					// Corner element is 1
					print("1");
				}
				else
				{
					// Intermediate element is 0
					print("0");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Pattern = Pattern();
	// Test
	task.printTriangle(7);
	task.printTriangle(12);
}

Output

Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001


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