patterns in C programming language | Type of Patterns with example

 


Introduction to Patterns in C Programming

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 access to memory, a simple set of keywords, and ease implementation are the main features of the C language. There are many languages like PHP, Java, Javascript, etc. follows features or syntax of C to some extent.

👉The Basic Structure of the C Language is given as follows

Header #include<stdio.h>
Main(): int main()
{
Variable Declaration: int x=12;
Body: printf(“%d”,x);
Return: return 0;
}

👉Examples

There are various patterns in C language like star pattern, number patterns, and character patterns. In this section, we are going to discussed how to create different patterns with the help examples.

1. Number Patterns

In this section, we are going to see how to print different number patterns in 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, 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:

Patterns in C Programming

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, 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 = 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:

Patterns in C Programming

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, 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:

Patterns in C Programming

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, 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:

Patterns in C Programming

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, 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:

Patterns in C Programming

2. Star Patterns

In this section, we are going to see how to print different star patterns in 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, 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:

Star patterns

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, 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:

Star patterns

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, 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:

Number Diamond Pattern

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, 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:

Star Triangle Pattern

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, 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:

Star Triangle Pattern

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, 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:

Character patterns

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, 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:

Characters Triangle Pattern

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, 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:

Inverted Triangle Pattern

 Recommended Article

This has been a guide to Patterns in C Programming. Here we discuss different numbers, stars and character’s Patterns with examples. You can also go through our other suggested articles to learn more 

Post a Comment

1 Comments