While writing the logic for patterns, we use loops in C to generate patterns. We can generate a specific pattern with numbers, characters, and symbols. Pattern program in C can be very useful in understanding loops, conditional statements, and other programming concepts. It can help you freshen up your nested loop concepts.
In this article, we will cover various pattern program in C, such as pyramid patterns, square patterns, Right angle triangles, etc. These patterns are also crucial from an interview point of view. Read the complete article to know more.
Pattern Program in CÂ
C language is a general-purpose language that was created by Dennis Ritchie in 1972. C language is easy to use. Simple keywords, library support, and vast communities are some of the essential features of C language. We can implement the concept of loops and conditional statements to make a pattern program. We can make various geometrical shapes using a pattern program, such as start patterns, pyramid patterns, square patterns, rectangle patterns, etc. Read the complete article to learn more.
Types of Pattern Program in C
We can present our program with numbers, special characters, and others. Let us learn about pattern program in C and their implementation using C language.
1. Pyramid Pattern Program in C
This program will print the pyramid shape using C language. We will ask users to enter the number of rows they want in their pattern.
                              Pyramid Pattern Program in C |
#include <stdio.h>
int main() {    int i, space, rows, k = 0;    printf(“Enter the number of rows: “);    scanf(“%d”, &rows);    for (i = 1; i <= rows; ++i, k = 0) {       for (space = 1; space <= rows – i; ++space) {          printf(” “);       }       while (k != 2 * i – 1) {          printf(“* “);          ++k;       }       printf(“\n”);    }    return 0; } |
Output:
2. Inverted Pyramid Pattern program in C
This program will print the inverted pyramid shape using the C language. We will ask users to enter the number of rows they want in their pattern.
                           Inverted Pyramid Pattern program in C |
#include <stdio.h>
int main() {    int rows, i, j, space;    printf (“Enter the number of rows: “);    scanf (“%d”, &rows);    for (i = rows; i >= 1; –i) {       for (space = 0; space < rows – i; ++space)          printf (” “);       for (j = i; j <= 2 * i – 1; ++j)          printf (“* “);       for (j = 0; j < i – 1; ++j)          printf(“* “);       printf(“\n”);    }    return 0; } |
Output
3. Floyd’s Triangle Pattern program in C
This program will print Floyd’s triangle shape using the C language. We will ask users to enter the number of rows they want in their pattern.
                                Floyd’s Triangle Pattern program in C |
#include <stdio.h>
int main() { Â Â Â int rows, i, j, number = 1; Â Â Â printf(“Enter the number of rows: “); Â Â Â scanf(“%d”, &rows); Â Â Â for (i = 1; i <= rows; i++) { Â Â Â Â Â Â for (j = 1; j <= i; ++j) { Â Â Â Â Â Â Â Â Â printf(“%d “, number); Â Â Â Â Â Â Â Â Â ++number; Â Â Â Â Â Â } Â Â Â Â Â Â printf(“\n”); Â Â Â } Â Â Â return 0; } |
Output:Â
4. Right Half Pyramid Pattern Program in CÂ
This program will print Right half pyramid triangle shape using the C language. We will ask users to enter the number of rows they want in their pattern.
                           Right Triangle Pattern program in C |
#include <stdio.h>
int main() { int rows = 7; // first loop for printing rows for (int i = 0; i < rows; i++) { // second loop for printing character in each rows for (int j = 0; j <= i; j++) { printf(“* “); } printf(“\n”); } return 0; } |
Output:Â
5. Diamond Pyramid Pattern Program in C
This program will print Right half pyramid triangle shape using the C language. We will ask users to enter the number of rows they want in their pattern.
                              Diamond Pattern program in C |
#include <stdio.h>int main()
{ Â Â Â Â Â Â Â Â Â Â int n; printf (“Enter the number of rows: “); scanf (“%d”, &n); // first outer loop to iterate through each row for (int i = 0; i < 2 * n – 1; i++) { int comp; if (i < n) { comp = 2 * (n – i) – 1; } else { comp = 2 * (i – n + 1) + 1; } // first inner loop to print leading whitespaces for (int j = 0; j < comp; j++) { printf(” “); } for (int k = 0; k < 2 * n – comp; k++) { printf(“* “); } printf(“\n”); } return 0; } |
Also read:Â Keywords and Identifiers in C
Output:Â
6. Hollow Pyramid Pattern Program in C
This program will print the Hollow pyramid triangle shape using the C language. We will ask users to enter the number of rows they want in their pattern.
                          Hollow Pyramid Pattern program in C |
#include <stdio.h>
int main() {     int rows;     printf(“Enter the number of rows: “);     scanf(“%d”,&rows);     // first outer loop to iterate through each row     for (int i = 0; i < rows; i++) {         // first inner loop to print leading whitespaces         for (int j = 0; j < rows – i – 1; j++) {             printf(” “);         }         // second inner loop to print stars * and inner whitespaces         for (int k = 0; k < 2 * i + 1; k++) {             if (k == 0 || k == 2 * i) {                 printf(“*”);             } else if (i == rows – 1) {                 printf(“*”);             } else {                 printf(” “);             }         }         printf(“\n”);     }     return 0; } |
Also read:Â Linear Search Algorithm in CÂ
Output:
7. Right Triangle Pattern Program in C
This program will print the Right Triangle pyramid triangle shape using characters in the C language. We will ask users to enter the number of rows they want in their pattern.
                            Right Triangle Pyramid Pattern program in C |
#include<stdio.h>
int main() {     int n;     printf(“Enter the number of rows: “);     scanf(“%d”,&n);     int k = 0;     for(int i = 0; i < n; i++)     {         for(int j = 0; j <= i; j++)         {             if((i + j) % 2 == 1)             {                 printf(“%c “,(char)(k + 97));             }             else             {                 printf(“%c “,(char)(k + 65));             }             k++;         }         printf(“\n”);     }     return 0; } |
Output:
Recommended Reads | |
Java Vs C++ Vs Python | Java or C++ which is better |
What is STL in C++ | Top Features of C++ programming language |
Pattern Program in C FAQs
Q1. Can we make patterns using C language?
Ans: We can draw many pattern programs in C using loops and conditional statements. Check the article for more details.
Q2. What are various patterns that I can make using the C language?
Ans: C language can be used to make various geometrical shapes such as stars, pyramids, diamonds, squares, etc.
Q3. How do I make number patterns using the C language?
Ans: You can easily make number patterns using loops such as for or while loops and conditional statements. However, the loops will be nested. Also, you can increment the number variable using a counter or otherwise keep it static.