C Program to print Right Angled Binary Pyramid
Print following Right Angled Binary Pyramid in C Programming :
1 2 3 4 5 |
<span style='color:#008c00'>1</span> <span style='color:#008c00'>0</span> <span style='color:#008c00'>1</span> <span style='color:#008c00'>1</span> <span style='color:#008c00'>0</span> <span style='color:#008c00'>1</span> <span style='color:#008c00'>0</span> <span style='color:#008c00'>1</span> <span style='color:#008c00'>0</span> <span style='color:#008c00'>1</span> <span style='color:#008c00'>1</span> <span style='color:#008c00'>0</span> <span style='color:#008c00'>1</span> <span style='color:#008c00'>0</span> <span style='color:#008c00'>1</span> |
Program to print Right Angled Binary Pyramid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int row, column; for (row = 0; row < 4; row++) { for (column = 0; column <= row; column++) { if (((row + column) % 2) == 0) { printf("0"); } else { printf("1"); } printf("\t"); } printf("\n"); } return 0; } |
Explanation Of Program :
In the above program we are using two for loops. Outer for loop will decide the line number and inner for loop will decide the total number of digits to be printed.
1 2 3 4 |
for (row = 0; row < 4; row++) { for (column = 0; column <= row; column++) { } } |
Inside the innermost for loop we are checking the following condition which will tell us the remainder. Depending on the value of remainder we are printing binary numbers such as 0 or 1.
1 2 3 4 5 |
if (((row + column) % 2) == 0) { printf("0"); } else { printf("1"); } |