How to

How to make C Program to print Patterns

C Program to print Patterns
Written by staff

C programming is not that tough, all you have to do is to understand the logic of program and then shape it. But the syntax and semantics causes a whole lot of trouble while compiling a program. Most of the C program to print patterns require utilization of nested loops and space. A pattern of no., staric or characters is a way of putting together these in a number of logical manner or they could form a sequence. A few of these C program patterns are triangles which have special importance in mathematical calculations. Some patterns are symmetrical while other are Asymmetrical.

Here are a few C programs for printing patterns.

C Program to print Patterns

C Program to print patterns:

*
**

***

****

******

To print pattern like this you will need the following code:

#include<stdio.h>
#include<conio.h>
void main()
{

int row,col;
for(row=1;row<=5;row++)

{
for(col=1;col<=row;col++)

{
printf(“*”,row,col);

}
printf(“\n”);
}

getche();
}

In the above program you can see 2 ‘for’ loops are being executed and the print statement is ‘*’. In order to get the same pattern with some other character you can simply change ‘*’ with any other.

1
12
123
1234

12345

For above pattern of numbers you can use this program.

#include<stdio.h>
#include<conio.h>
void main()
{

int row,col;
for(row=1;row<=5;row++)

{
for(col=1;col<=row;col++)

{
printf(“%d”,col);

}
printf(“\n”);
}
getche();

}

              1

           232

         34543

       4567654

      567898765

In order to print something like that you can use the following with ‘for’ nested loops.

#include<stdio.h>

main()
{
int n, c, d, num = 1, space;

scanf(“%d”,&n);

space = n – 1;

for ( d = 1 ; d <= n ; d++ )
{
num = d;

for ( c = 1 ; c <= space ; c++ )
printf(” “);

space–;

for ( c = 1 ; c <= d ; c++ )
{
printf(“%d”, num);
num++;
}
num–;
num–;
for ( c = 1 ; c < d ; c++)
{
printf(“%d”, num);
num–;
}
printf(“\n”);

}

return 0;
}

Now in the program we have used ‘scanf’ function too so it will print the as many rows as you will enter in to print. For example the above pattern is for 5 rows.

So using the same logics you can create a no. of patterns. If you are having trouble related to pattern printing program drop a comment below.

Leave a Comment