/*
Name: WAP to print binary triangle
Author: Parveen Malik
Date: 18/09/11 05:40
*/
#include<stdio.h>
int main()
{
int i,j,row,mod,temp=1;
char inputstr[35]; /* make longer than reasonable input will expected */
/* error checking should still be employed */
/* to clear buffer overflow, if input is too long */
while ( temp==1)
{
row=0;
while(row<=0)
{
printf("\n Enter the number of rows you want : ");
fflush(stdout); /* printf didn't have newline, need to flush */
fgets(inputstr,34,stdin);
/* error checking should go here for buffer overflow */
row = atoi (inputstr);
if(row<=0)
printf("invalid input, try again \n");
}
for ( i=0;i<=row-1;i++)
{
mod=i%2;
for(j=0;j<=i;j++)
{
printf("%d",mod);
if(mod==0)
mod=1;
else
mod=0;
}
printf("\n");
}
}
return 0;
}
OUTPUT:
0 comments:
Post a Comment