Written by Sai Sameera Thota on October 1, 2013 in C > Programming

C Program to print Pattern

GET ALERTS:

Get our Latest updates delivered to your mailbox!

This program takes number of lines as input and displays the following pattern as output

1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
0 1 0 1 0 1

The program uses two for loops. The first for loop is used to determine the line number and the second for
loop is used to print either 0 or 1. If the line number is odd, then the pattern should begin with 1 or else 0.
The value of ‘c’ changes from 0 to 1 and vice-versa for every iteration of second loop. If the value of c is 1 it
will change to 0 and vice-versa.

C Program to print Pattern

#include<stdio.h>
main()
{
	int i,j,c=0,n;
	printf("Enter the number of lines : ");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	
	{
		if(i%2==0)
			c=1;
		else
			c=0;
		for(j=1;j<=i;j++)
		{
			if(c==1)
				c=0;
			else
				c=1;
			printf("%d  ",c);
		}
		printf("\n");
	}
	getch();
}

Download Source Code

Output:
c_program_print_pattern

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: