Updated July 3, 2023
Introduction to Patterns in C Programming
C is the procedural, general-purpose programming language. It was first created between 1969 and 1973 by Dennis Ritchie. Low-level memory access, a simple set of keywords, and easy implementation are the main features of the C language. Many languages, like PHP, Java, Javascript, etc., follow the features or syntax of C to some extent. In this article, we will cover Patterns in C Programming.
Structure of the C Language
Header #include<stdio.h>
Main(): int main()
{
Variable Declaration: int x=12;
Body: printf(“%d”,x);
Return: return 0;
}
Types of Patterns in C Programming
There are various patterns in the C language, like star patterns, number patterns, and character patterns. In this section, we are going to discuss how to create different patterns with the help of examples.
1. Number Patterns
This section will show how to print different number patterns in the C language.
Example 1: Program in C to print the number pyramid pattern
In the following C program, the user can provide the number of rows to print the number pyramid pattern as he wants, and the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, x, y, k;
printf("Enter the number of rows to show number pattern: ");
scanf("%d",&n);
for(x =1; x <= n; x++)
{
for(y =1; y <= n; y++)
{
if(y <= x)
printf("%d",y);
else
printf(" ");
}
for(y = n; y >= 1;y--)
{
if(y <= x)
printf("%d",y);
else
printf(" ");
}
printf("\n");
}
return 0;
}
Output:
Example2: Program in C to print the Number Diamond Pattern
In the following C program, the user can provide the number of rows to print the diamond pattern as he wants, and the result will be displayed on the screen
#include<stdio.h>
#include<conio.h>
int main()
{
int n, x, y, k;
printf("Enter the number of rows to show number paatern: ");
scanf("%d",&n);
for(x = 1; x <= n; x++)
{
for(y = x; y <n; y++)
{
printf(" ");
}
for(k = 1; k < (x*2); k++)
{
printf("%d",k);
}
printf("\n");
}
for(x = 4; x >= 1; x--)
{
for(y = n; y > x; y--)
{
printf(" ");
}
for(k = 1; k < (x*2); k++)
{
printf("%d",k);
}
printf("\n");
}
return 0;
}
Output:
Example 3: Program in C to print the number pyramid pattern
In the following C program, the user can provide the number of rows to print the number pyramid pattern as he wants, and the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int x, s, n, y = 0, cntr = 0, cntr1 = 0;
printf("Enter the number of rows to show number pattern: ");
scanf("%d",&n);
for(x = 1; x <= n; ++x)
{
for(s = 1; s <= n-x; ++s)
{
printf(" ");
++cntr;
}
while(y != 2 * x - 1)
{
if (cntr <= n - 1)
{
printf("%d ", x + y);
++cntr;
}
else
{
++cntr1;
printf("%d ", (x + y - 2 * cntr1));
}
++y;
}
cntr1 = cntr = y = 0;
printf("\n");
}
return 0;
}
Output:
Example 4: Program in C to print the vertical number pyramid pattern
In the following C program, the user can provide the number of rows to print the vertical number pyramid pattern as he wants, and the result will be displayed on the screen
#include<stdio.h>
#include<conio.h>
int main()
{
int n, x, y;
printf("Enter the number of rows to show number pattern: ");
scanf("%d",&n);
for(int x = 1; x < n; x++)
{
for(int y = 1; y <= x; y++)
printf("%d",y);
printf("\n");
}
for(int x = n; x >= 0; x--)
{
for(int y = 1; y <= x; y++)
printf("%d",y);
printf("\n");
}
return 0;
}
Output:
Example 5: Program in C to print the number pyramid pattern
In the following C program, the user can provide the number of rows to print the number pyramid pattern as he wants, and the result will be displayed on the screen
#include<stdio.h>
#include<conio.h>
int main()
{
int n, x, y;
printf("Enter the number of rows to show number patterns: ");
scanf("%d",&n);
for (int x = n; x >= 0; x--)
{
for (int y = 1; y <= x; y++)
printf("%d",y);
printf("\n");
}
for(int x = 1; x <= n; x++)
{
for(int y = 1; y <= x; y++)
printf("%d",y);
printf("\n");
}
return 0;
}
Output:
2. Star Patterns
In this section, we are going to see how to print different Star patterns in the C language
Example 1: Program in C to print the Star Diamond Pattern
In the following C program, the user can provide the number of rows to print the Star Diamond pattern as he wants, and the result will be displayed on the screen
#include<stdio.h>
#include<conio.h>
int main()
{
int n, s, x, y;
printf("Enter number of rows to show star pattern: ");
scanf("%d",&n);
for(x = 0; x <= n; x++)
{
for(s = n; s > x; s--)
printf(" ");
for(y = 0; y < x; y++)
printf("* ");
printf("\n");
}
for(x = 1; x < n; x++)
{
for(s = 0; s < x; s++)
printf(" ");
for(y = n; y > x; y--)
printf("* ");
printf("\n");
}
return 0;
}
Output:
Example 2: Program in C to print the Vertical Curve Pattern
In the following C program, the user can provide the number of rows to print the Vertical Curve pattern as he wants, and the result will be displayed on the screen
#include<stdio.h>
#include<conio.h>
int main()
{
int n, x , y;
printf("Enter number of rows to show star pattern: ");
scanf("%d",&n);
for(x = 1; x <= n; x++)
{
for(y = 1; y <= x; y++)
{
printf("*");
}
printf("\n");
}
for(x = n; x >= 1; x--)
{
for(y = 1; y <= x; y++)
{
printf( "*");
}
// ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 3: Program in C to print the Hollow Number Diamond Pattern
In the following C program, the user can provide the number of rows to print the hollow diamond pattern as he wants, and the result will be displayed on the screen
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Enter the number of rows to show the star pattern: ");
int n, x, y, s = 1, k;
scanf("%d",&n);
for(x = 0; x <= n; x++)
{
for(y = n; y > x; y--)
{
printf(" ");
}
printf("*");
if (x > 0)
{
for(k = 1; k <= s; k++)
{
printf(" ");
}
s += 2;
printf("*");
}
printf("\n");
}
s -= 4;
for(x = 0; x <= n -1; x++)
{
for(y = 0; y <= x; y++)
{
printf(" ");
}
printf("*");
for(k = 1; k <= s; k++)
{
printf(" ");
}
s -= 2;
if(x != n -1)
{
printf ("*");
}
//ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 4: Program in C to print the Hollow Star Triangle Pattern
In the following C program, the user can provide the number of rows to print the Hollow Star Triangle pattern as he wants, and the result will be displayed on the screen
#include<stdio.h>
#include<conio.h>
int main()
{
int n, x, y, s;
printf("Enter number of rows to show the star pattern: ");
scanf("%d",&n);
for(x = 1; x <= n; x++)
{
//for loop to put space in pyramid
for (s = x; s < n; s++)
printf(" "); //for loop to print star
for(y = 1; y <= (2 * n - 1); y++)
{
if(x == n || y == 1 || y == 2 * x - 1)
printf("*");
else
printf(" ");
}
//ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 5: Program in C to print the Star Triangle Pattern
In the following C program, the user can provide the number of rows to print the Star Triangle pattern as he wants, and the result will be displayed on the screen
#include<stdio.h>
#include<conio.h>
int main()
{
int n, s, x, y;
printf("Enter number of rows to show star pattern: ");
scanf("%d",&n);
for(x = 1; x <= n; x++)
{
//for loop to put space
for(s = x; s < n; s++)
printf(" ");
//for loop for displaying star
for(y = 1; y <= x; y++)
printf("* ");
// ending line after each row
printf("\n");
}
return 0;
}
Output:
3. Character Patterns
In this section, we are going to see how to print different character patterns in C language
Example 1: Program in C to print the Consecutive Characters’ Triangle Pattern
In the following C program, the user can provide the number of rows to print the Consecutive Characters’ Triangle Triangle pattern as he wants, and the result will be displayed on the screen
#include<stdio.h>
#include<conio.h>
int main()
{
int n, x, y;
printf("Enter number of rows to show character pattern: ");
scanf("%d",&n);
for(x = 1; x <= n; x++)
{
for(y = 1; y <= x; y++)
{
printf("%c",'A' + y -1);
}
printf("\n");
}
return 0;
}
Output:
Example 2: Program in C to print the Characters’ Triangle Pattern
In the following C program, the user can provide the number of rows to print the Characters’ Triangle Triangle pattern as he wants, and the result will be displayed on the screen
#include<stdio.h>
#include<conio.h>
int main()
{
int n, x, y;
printf("Enter number of rows to show character pattern: ");
scanf("%d",&n);
for(x = 1; x <= n; x++)
{
for(y = 1; y <= x; y++)
{
printf("%c",'A'-1 + x);
}
printf("\n");
}
return 0;
}
Output:
Example 3: Program in C to print the Characters’ Inverted Triangle Pattern
In the following C program, the user can provide the number of rows to print the Characters’ Inverted Triangle pattern as he wants, and the result will be displayed on the screen
#include<stdio.h>
#include<conio.h>
int main()
{
int n, x, y;
printf("Enter number of rows to show character pattern: ");
scanf("%d",&n);
for(x= 1; x <= n; x++)
{
for(y = n; y >= x; y--)
{
printf("%c",'A'-1 + x);
}
printf("\n");
}
return 0;
}
Output:
Recommended Article
This has been a guide to Patterns in C Programming. Here we discuss different numbers, stars, and characters’ Patterns with examples. You can also go through our other suggested articles to learn more –