Q. Write a C program to print the following 0 and 1 number triangle:
1
01
010
1010
10101
Ans.
/*c 0 and 1 number triangle program*/
#include<stdio.h>
int main()
{
int r,c,num,v1=1;
printf("Enter number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(c=1; c<=r; c++)
{
printf("%d",v1);
if(v1==1)
v1=0;
else
v1=1;
}
printf("\n");
}
getch();
return 0;
}
The output of above program would be:
1
01
010
1010
10101
Ans.
/*c 0 and 1 number triangle program*/
#include<stdio.h>
int main()
{
int r,c,num,v1=1;
printf("Enter number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(c=1; c<=r; c++)
{
printf("%d",v1);
if(v1==1)
v1=0;
else
v1=1;
}
printf("\n");
}
getch();
return 0;
}
The output of above program would be:
Figure: Screen shot for 0 and 1 number triangle C porgram |
Comments
Post a Comment