In this program, we are going to make pattern in c programming using loops. It gives us a better understanding of the implementation of loops.
Pre-requisite: For/While loop
Half Pyramid Pattern in C
In this program, we will print half pyramid of numbers.
11 2
1 2 3
1 2 3 4
In the above pattern. we can observe that there are four rows and ith row has i columns. We will use 2 loops, the outer loop will iterate through rows and the inner loop will print numbers. It is also known as the triangular pattern in c.
#include <stdio.h>
int main() {
int i=1;
while(i<=4)
{
int j=1;
while(j<=i){
printf("%d ",j);
j++;
}
printf("\n");
i++;
}
return 0;
}
Right Triangle Star Pattern in C
In this program, we have to just print star "*" at the place of numbers.
** *
* * *
* * * *
Pattern Program in C using for loop
#include <stdio.h>
int main() {
int n=4;
int i=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
printf("* ");
printf("\n");
}
return 0;
}
Square Pattern Program in c using while loop
In this program, we will print a square pattern in c using while loop. We will run two loops as usual one for rows and another one is to print star in columns.
* * * ** * * *
* * * *
* * * *
#include <stdio.h>
int main() {
int n=4;
int i=1;
while(i<=n)
{
int j=1;
while(j<=n){
printf("* ");
j++;
}
printf("\n");
i++;
}
return 0;
}
C program to print alphabet pattern
This program is similar to a triangular pattern of numbers. In this example, we will print characters instead of numbers.
aa b
a b c
a b c d
#include <stdio.h>
int main() {
int n=4;
int i=1;
while(i<=n)
{
int j=1;
char ch='a';
while(j<=i){
printf("%c ",ch);
ch++;
j++;
}
printf("\n");
i++;
}
return 0;
}
Triangular Pattern in c
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 9; j++)
{
if ((j <= 5 - i) || (j >= 5 + i))
{
printf(" ");
}
else
{
printf("*");
}
}
printf("\n");
} *
***
*****
*******
*********
About This C Program To Print Simple Pattern:
In conclusion, we made different pattern in c programming using for loop and while loop. However, if you have doubt or need code for a different pattern let me know in the comment section. These programs give us a better clarity of loops, iterating through rows and columns.
Related Posts:
C Program to Print X Pattern
C program to print diamond pattern of numbers