Print binary right angle triangle
In this article, we will discuss the problem of printing a binary right angle triangle using a simple C program. The program will take a height as input and generate a right angle triangle using binary values.
Problem Statement
The problem is to create a program that prints a right angle triangle using binary values. The triangle should have a height specified by the user, and each row of the triangle should contain alternating 0s and 1s.
For example, if the user inputs a height of 5, the program should generate the following triangle:
Height : 5 0 1 0 0 1 0 1 0 1 0 0 1 0 1 0
The triangle starts with a 0 in the first row, and each subsequent row alternates between 0s and 1s.
Approach and Algorithm
To solve this problem, we can use nested loops. The outer loop will iterate over the rows of the triangle, and the inner loop will iterate over the columns of each row.
Here is the algorithm to print the binary right angle triangle:
- Start with an input height from the user.
- Iterate from 0 to height-1 (outer loop) to represent each row of the triangle.
- For each row, iterate from 0 to the current row number (inner loop) to represent the columns.
- Inside the inner loop, calculate the value to be printed by using the bitwise AND operator (&) between the row number and the column number.
- If the result of the bitwise AND is 0, print 0; otherwise, print 1.
- Print a newline character after each row is printed.
- Repeat steps 2-6 for all rows.
- End of the program.
Pseudocode
Based on the above algorithm, here is the pseudocode for the program:
function printPattern(height)
{
for i from 0 to height-1
{
for j from 0 to i
{
print (i + j) & 1
}
print newline
}
}
main()
{
printPattern(5)
printPattern(9)
printPattern(7)
printPattern(8)
}
Code Solution
// 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
Explanation and Result
Let's understand the code and the output it produces for different heights.
The code starts with the printPattern
function that takes the height as an argument. It then uses nested loops to iterate over the rows and columns of the triangle. The bitwise AND operation between the row number (i
) and the column number (j
) determines the value to be printed: 0 or 1. The result is printed for each column, and a newline character is printed after each row.
The main
function calls the printPattern
function with different heights to generate multiple triangles.
For example, when the height is 5, the program outputs:
Height : 5 0 1 0 0 1 0 1 0 1 0 0 1 0 1 0
As we can see, the generated triangle has a height of 5, and each row alternates between 0s and 1s.
The program produces similar output for other heights as well.
Time Complexity
The time complexity of this program is O(n^2)
, where n
is the height of the triangle. This is because we have nested loops that iterate from 0 to height-1
and from 0 to i
for each row. As the height increases, the number of iterations increases quadratically.
Finally
In this article, we discussed how to print a binary right angle triangle using a simple C program. We explained the problem statement, provided an algorithm and pseudocode, and analyzed the output generated by the code. The program uses nested loops and bitwise operations to determine the binary values to be printed for each row and column of the triangle. We also discussed the time complexity of the program.
By understanding this program, you can gain insights into nested loop structures, bitwise operations, and pattern printing techniques in programming.
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