Problem Statement : Generate Following Pattern of Pyramid
#include<stdio.h> #include<conio.h> void main() { int i,j; int count = 1; clrscr(); for(i=1;i<=4;i++) { printf("\n"); for(j=1;j<=i;j++) { printf("%d",count%2); count++; } if( i % 2 == 0) count=1; else count=0; } getch(); }
How to Build Logic ?
- As You can see , First and Third Line is Starting with 1 , while 2nd and 4th Line is starting with 0
- Note : So for First and Third Line count=1 and count = 0 for 2nd and 4th Line
- Outer For Loop for printing 4 different lines , when the Outer Loop is executed then always Print newline(n)
- Inner Loop is for Printing either 0 or 1 . if we are on second line then count = 0.
Page included in Program >> Pyramid Section