Get the following output:
0
1 0
0 1 0
1 0 1 0
0 1 0 1 0
Source code : Using for loop
#include<stdio.h> int main() { int i,j,k; for(i=1;i<=5;i++) { if(i%2==1) k=0; else k=1; for(j=1;j<=i;j++) { printf("%d ",k); if(k==0) k=1; else k=0; } printf("\n"); } return(0); }
Source code : Using while loop
#include<stdio.h> int main() { int i=1,j,k; while(i<=5) { if(i%2==1) k=0; else k=1; j=1; while(j<=i) { printf("%d ",k); if(k==0) k=1; else k=0; j++; } printf("\n"); i++; } return(0); }
Get the following output:
1
1 0 1
1 0 1 0 1
1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1
Source code : Using for loop
#include<stdio.h> int main() { int i,j,k,n=5,m; for(i=1;i<=5;i++) { for(m=1;m<=n;m++) printf(" "); k=1; //left side for(j=1;j<=i;j++) { printf("%d ",k); if(k==0) k=1; else k=0; } //right side for(j=i-1;j>=1;j--) { printf("%d ",k); if(k==0) k=1; else k=0; } printf("\n"); n--; } return(0); }
Source code : Using while loop
#include<stdio.h> int main() { int i=1,j,k,n=5,m; while(i<=5) { m=1; while(m<=n) { printf(" "); m++; } k=1; j=1; //left while(j<=i) { printf("%d ",k); if(k==0) k=1; else k=0; j++; } //right j=i-1; while(j>=1) { printf("%d ",k); if(k==0) k=1; else k=0; j--; } printf("\n"); i++; n--; } return(0); }
Get the following output:
0
0 1 0
0 1 0 1 0
0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
Source code : Using for loop
#include<stdio.h> int main() { int i,j,k,n=5,m; for(i=1;i<=5;i++) { for(m=1;m<=n;m++) printf(" "); k=0; //left side for(j=1;j<=i;j++) { printf("%d ",k); if(k==0) k=1; else k=0; } //right side for(j=i-1;j>=1;j--) { printf("%d ",k); if(k==0) k=1; else k=0; } printf("\n"); n--; } return(0); }
Source code : Using while loop
#include<stdio.h> int main() { int i=1,j,k,n=5,m; while(i<=5) { m=1; while(m<=n) { printf(" "); m++; } k=0; j=1; //left while(j<=i) { printf("%d ",k); if(k==0) k=1; else k=0; j++; } //right j=i-1; while(j>=1) { printf("%d ",k); if(k==0) k=1; else k=0; j--; } printf("\n"); i++; n--; } return(0); }