Pattern Programs in C

Printing patterns using C programs has always been an interesting problem domain. We can print different patterns like star patterns, pyramid patterns, Floyd’s triangle, Pascal’s triangle, etc. in C language. These problems generally require the knowledge of loops and if-else statements.

In this article, we will discuss the following example programs for printing patterns in the C programming language .

pattern-program-in-c

Pattern Programs in C

Table of Content

1. Right Half Pyramid Pattern in C

The right-half pyramid is nothing but a right-angle triangle whose hypotenuse is in the right direction. We can print the right half pyramid pattern using numbers, alphabets, or any other character like a star (*).

Example:

Output

* | 1 | A
* * | 1 2 | A B
* * * | 1 2 3 | A B C
* * * * | 1 2 3 4 | A B C D
* * * * * | 1 2 3 4 5 | A B C D E

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

2. Left Half Pyramid Pattern in C

The Left Half Pyramid looks like a right-angled triangle with its hypotenuse facing the left. We can also print this pattern using a character, alphabets, or numbers.

Example:

Output

 * | 1 | A
* * | 1 2 | A B
* * * | 1 2 3 | A B C
* * * * | 1 2 3 4 | A B C D
* * * * * | 1 2 3 4 5 | A B C D E

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

3. Full Pyramid Pattern in C

The Full Pyramid pattern looks similar to the Equilateral triangle. We can see this as the combination of the Left Half and Right Half pyramids patterns. The following example demonstrates how to print this pattern using alphabets, numbers, or a star (*).

Example:

Output

 * | 1 | A
* * * | 1 2 3 | A B C
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

4. Inverted Right Half Pyramid Pattern in C

This pattern is the 180° flipped version of the Right Half Pyramid Pattern we discussed earlier.

Example:

Output

* * * * * | 1 2 3 4 5 | A B C D E 
* * * * | 1 2 3 4 | A B C D
* * * | 1 2 3 | A B C
* * | 1 2 | A B
* | 1 | A

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

5. Inverted Left Half Pyramid Pattern in C

This pattern is the 180° flipped version of the left half pyramid pattern we discussed earlier.

Example:

Output

* * * * * | 1 2 3 4 5 | A B C D E
* * * * | 1 2 3 4 | A B C D
* * * | 1 2 3 | A B C
* * | 1 2 | A B
* | 1 | A

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

6. Inverted Full Pyramid Pattern in C

It is a 180° flipped version of the Full Pyramid Pattern we discussed earlier. We can see this as the combination of the Inverted Left Half and Inverted Right Half Pyramid Pattern we discussed earlier.

Example:

Output

* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * | 1 2 3 4 5 | A B C D E
* * * | 1 2 3 | A B C
* | 1 | A

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

7. Rhombus Pattern in C

The Rhombus pattern is similar to the square pattern, just that we have to add spaces before each line and their count decreases progressively with rows.

Example:

Output

 * * * * * | 1 2 3 4 5 | A B C D E
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * | 1 2 3 4 5 | A B C D E

8. Diamond Pattern in C

The Diamond Pattern is obtained by joining the Full Pyramid and Inverted Full Pyramid Pattern by their bases. We can also print this pattern using any character.

Example:

Output

 * | 1 | A
* * * | 1 2 3 | A B C
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * | 1 2 3 4 5 | A B C D E
* * * | 1 2 3 | A B C
* | 1 | A

9. Hourglass Pattern in C

Hourglass Pattern is a combination of the inverted full pyramid and full pyramid patterns but in the opposite sense to that of diamond pattern. Here we join them using their tip.

Example:

Output

* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * | 1 2 3 4 5 | A B C D E
* * * | 1 2 3 | A B C
* | 1 | A
* * * | 1 2 3 | A B C
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I

10. Hollow Square Pattern in C

The Hollow Square Pattern is a square with only the boundary lines. The space inside should be empty in this pattern.

Example

Output

* * * * * | 1 2 3 4 5 | A B C D E
* * | 1 5 | A E
* * | 1 5 | A E
* * | 1 5 | A E
* * * * * | 1 2 3 4 5 | A B C D E

11. Hollow Full Pyramid Pattern

In the Hollow Pyramid pattern, we only have to print the boundary of the full pyramid.

Example:

Output

 * | 1 | A
* * | 1 3 | A C
* * | 1 5 | A E
* * | 1 7 | A G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I

12. Hollow Inverted Full Pyramid Pattern

In this pattern, we print the inverted full pyramid with only boundary elements and remove the inside elements to make it hollow.

Example:

Output

* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I
* * | 1 7 | A G
* * | 1 5 | A E
* * | 1 3 | A C
* | 1 | A

13. Hollow Diamond Pattern

This pattern is also similar to the Diamond Pattern but without the inner elements such that it appears hollow inside.

Example:

Output

 * | * | A
* * | * * | A C
* * | * * | A E
* * | * * | A G
* * | * * | A I
* * | * * | A G
* * | * * | A E
* * | * * | A C
* | * | A

14. Hollow Hourglass Pattern in C

The hollow hourglass is the pattern in which only the boundary of the hourglass pattern is visible.

Example:

Output

* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I
* * | 1 7 | A G
* * | 1 5 | A E
* * | 1 3 | A C
* | 1 | A
* * | 1 3 | A C
* * | 1 5 | A E
* * | 1 7 | A G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I

15. Floyd’s Triangle in C

In Floyd’s Triangle pattern, instead of starting the sequence of the numbers from 1 in each row, we print consecutive natural numbers. We can also print this pattern for alphabet sequence.

Example:

Output

1 | A
2 3 | B C
4 5 6 | D E F
7 8 9 10 | G H I J

16. Pascal’s Triangle in C

A Pascal’s Triangle is a triangular array of binomial coefficients where the n th row contains the binomial coefficients n C 0 , n C 1 , n C 2 , ……. n C n . The following example demonstrates one of the methods using which we can print Pascal’s Triangle Pattern.

Example:


Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

To know more about Pascal’s Triangle, refer to this article – Pascal’s Triangle

Related Articles:

Like Article -->

Please Login to comment.

Similar Reads

Common Memory/Pointer Related bug in C Programs

Dereferencing an unknown memory location : C programmers mostly use scanf() function to take input, but sometimes a small mistake can bring a bug or even crash whole program. The syntax for scanf() is scanf("%d", &a);. It might be possible to miss a & and write &a as a so now scanf("%d", a); is dereferencing to an unknown location. Now

6 min read Output of C programs | Set 63

Prerequisite : Structure padding, integer promotion and sequence points. Q1. Consider the following code: C/C++ Code #include<stdio.h> struct geeks < int i; char c; >obj; int main() < printf("%ld", sizeof(obj)); >What would be the output of the above code? A. 4 B. 5 C. 8 D. Can't be determined Output: Can't be determined Explanatio

4 min read Facts and Question related to Style of writing programs in C/C++

Here are some questions related to Style of writing C programs: Question-1: Why i++ executes faster than i + 1 ? Answer-1: The expression i++ requires a single machine instruction such as INR to carry out the increment operation whereas, i + 1 requires more instructions to carry out this operation. Question-2: Is writing if(! strcmp(s1, s2) ) a goo

2 min read Output of C programs | Set 66 (Accessing Memory Locations)

Q1. Is the output of this code True or False? #include <stdio.h> int main(void) < int b = 20; int* y = &b; char n = 'A'; char* z = &n; y[0] = z[0]; printf((*y == *z) ? "True" : "False"); >A. True B. False C. Program would crash D. Compilation error Answer: A. True Explanation: The binary form of 20 is 10100 which

7 min read CLI programs in C for playing media and shut down the system

Command Line Interface: CLI is a text-based user interface (UI) used to view and manage computer files.Command-line interfaces are also called command-line user interfaces, the console uses interfaces and characters uses interfaces.In programming, the user gives input during the execution of a program i.e., in the running phase. But there are a lot

6 min read Programs to print Interesting Patterns

Program to print the following pattern: Examples : Input : 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This program is divided into four parts. C/C++ Code // C++ program to print // the given pattern #include<iostream> using namespace std; void pattern(int n

15+ min read Output of C programs | Set 30 (Switch Case) 2 min read C/C++ Tricky Programs

We may come across various tricky programs in our day-to-day life. Maybe in technical interviews, coding tests, or C/C++ classrooms. Here is a list of such programs:- Print text within double quotes (" "). This may seem easy, but beginners may get puzzled while printing text within double quotes. C/C++ Code #include <stdio.h> int main() < pri

6 min read C File Handling Programs

C Program to list all files and sub-directories in a directory C Program to count number of lines in a file C Program to print contents of file C Program to copy contents of one file to another file C Program to merge contents of two files into a third file C program to delete a file

1 min read Memory Layout of C Programs

A typical memory representation of a C program consists of the following sections. Text segment (i.e. instructions)Initialized data segment Uninitialized data segment (bss)Heap Stack A typical memory layout of a running process 1. Text Segment: A text segment, also known as a code segment or simply as text, is one of the sections of a program in

6 min read C Programs

To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and more. These C Examples cover a range of questions,

8 min read Program to print numeric pattern

Given the value of n i.e, the number of rows, print the following pattern. Examples : Input : n = 4 Output : 1 5 2 8 6 3 10 9 7 4 Input : n = 6 Output : 1 7 2 12 8 3 16 13 9 4 19 17 14 10 5 21 20 18 15 11 6 Approach: The approach is to start printing the pattern from the end of each row. After the completion of the last column of each row, start fr

6 min read Program to print the Cot Bed Pattern

The given task is to draw the Cot Bed pattern using '$' Cot Bed Pattern: $$$$$$$$$$$$$$$$$$$$$$$$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$$$$$$$$$$$$$$$$$$$$$$$$ Below is the code to implement the above problem: Program: C/C++ Code // C++ program to print // the Cot Bed Pattern // header files #include <b

10 min read Magical Pattern

Given an integer N as input, the task is to print the Magical Pattern as given below: N . . 3 2 1 2 3 . . N . . . . . . . . . . . 3 3 3 3 2 1 2 3 3 3 3 2 2 2 2 2 1 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 1 2 2 2 2 2 3 3 3 3 2 1 2 3 3 3 3 . . . . . . . . . . . N . . 3 2 1 2 3 . . N Examples: Input: 3 Output: 3 2 1 2 3 2 2 1 2 2 1 1 1 1 1 2 2 1 2 2

9 min read C Program for KMP Algorithm for Pattern Searching[duplicate]

C/C++ Code // C++ program for implementation of KMP pattern searching // algorithm #include <bits/stdc++.h> void computeLPSArray(char* pat, int M, int* lps); // Prints occurrences of txt[] in pat[] void KMPSearch(char* pat, char* txt) < int M = strlen(pat); int N = strlen(txt); // create lps[] that will hold the longest prefix suffix // value

2 min read C Program for Rabin-Karp Algorithm for Pattern Searching

Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n > m. Examples: Input: txt[] = "THIS IS A TEST TEXT" pat[] = "TEST" Output: Pattern found at index 10 Input: txt[] = "AABAACAADAABAABA" pat[] = "AABA" Output: Pattern found at ind

3 min read Substring Reverse Pattern

Given string str, the task is to print the pattern given in the examples below: Examples: Input: str = "geeks" Output: geeks *kee* **e** The reverse of "geeks" is "skeeg" Replace the first and last characters with '*' i.e. *kee* Replace the second and second last character in the modified string i.e. **e** And so on. Input: str = "first" Output: fi

5 min read Program to print half Diamond star pattern

Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print them separately with the he

4 min read C Program to Print Continuous Character Pattern

Here, we will see how to print continuous character patterns using a C program. Below are the examples: Input: rows = 5Output: A B C D E F G H I J K L M N O Input: rows = 3Output: A B C D E F There are 2 ways to print continuous character patterns in C: Using for loop.Using while loop. Let's discuss each of these in detail. 1. Using for loop Approa

5 min read C Program For Printing Half Right Number Pyramid Pattern

Here, we will see a C program to print the 3 different number patterns. There are 3 number patterns covered using for loop and while loop with their respective explanation. 3 Different Number Patterns: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5Pattern 1: Input: n = 5 Output: 1 2 3 4 5 6 7 8 9 10

6 min read C Program To Print Floyd Triangle Pattern

Here, we will build a C Program To Print Floyd Triangle Pattern using 2 approaches i.e. Using for loop Using while loop Input: n = 5 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1. Using for loop The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. Then print the number and increme

2 min read C Program To Print Diamond Pattern

Here, we will see how to print a full diamond shape pyramid using the C program. Below are the examples: Input: 6Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Input: 8Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

3 min read C Program To Print Reverse Floyd's Pattern

Here we will develop a C Program To Print The Reverse Floyd pattern. Given the value of row(number of rows), we need the approach of looping and using Nested Loop to print this pattern. Input: row = 5 Output: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Using the concepts of the Nested loop The approach is to previously calculate the maximum value which is

2 min read C Program For Printing Simple Half Right Star Pyramid Pattern

Here, we will develop a C Program To Print Simple Half Right Star Pyramid Pattern using two approaches i.e. Using for loop Using while loop Input: rows = 5 Output: * * * * * * * * * * * * * * * 1. Using for loop: First for loop is used to identify the number of rows and the second for loop is used to identify the number of columns. Here the values

2 min read C Program To Print Character Pyramid Pattern

Here, we will build a C Program To Print Character Pyramid Pattern using 2 approaches i.e. Using for loopUsing while loop Input: rows = 5 Output: A B B C C C D D D D E E E E E 1. Using for loopApproach 1: Assign any character to one variable for the printing pattern The first for loop is used to iterate the number of rows and the second for loop is

5 min read C Program To Print Continuous Character Pyramid Pattern

There are 2 ways to print continuous character patterns in C i.e: Using for loopUsing while loop Input: rows = 5 Output: A B C D E F G H I J K L M N O1. Using for loopApproach 1: Using CharacterAssign any character to one variable for the printing pattern. The first for loop is used to iterate the number of rows.The second for loop is used to repea

5 min read C Program To Print Hollow Diamond Pattern

To print the hollow diamond pattern in C, we will use the following 2 approaches: for Loopwhile Loop Input: n = 5 Output: * * * * * * * * * * * * * * * *Approach 1: Using for loop Example: C/C++ Code // C Program To Print Hollow Diamond // Pattern using for loop #include <stdio.h> int main() < int n = 5, rows, columns; // for loop is used to

3 min read C Program to Print Cross or X Number Pattern

Write a C program to print X number pattern series using a loop. How to print the X number pattern series using for loop in C program. Logic to Print X using numbers in C programming Input N = 5 Output: The pattern consists of Exactly n*2-1 rows and columns. Hence outer loop run rows with(for i=1;i<=count;i++) and run inner loop as for(j=1;j<

1 min read C Program for Naive algorithm for Pattern Searching

Write a C program for a given text string with length n and a pattern with length m, the task is to print all occurrences of the pattern in text.Note: You may assume that n > m. Examples: Input: text = “THIS IS A TEST TEXT”, pattern = “TEST”Output: Pattern found at index 10 Input: text = “AABAACAADAABAABA”, pattern = “AABA”Output: Pattern found

2 min read C Program for KMP Algorithm for Pattern Searching

Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n > m. Examples: Input: txt[] = "THIS IS A TEST TEXT" pat[] = "TEST" Output: Pattern found at index 10 Input: txt[] = "AABAACAADAABAABA" pat[] = "AABA" Output: Pattern found at ind