Javatpoint Logo
Javatpoint Logo

Pattern Programs in C

It's always fun to try to create something different by programming. Creating patterns using programming languages will be the first step you take before making more creative items. In this article, we will discuss how to create some fun patterns using C programming language.

In this article, we will discuss about the following patterns:

  1. Semi-pyramid of *
  2. Semi-pyramid of numbers
  3. Semi-pyramid of alphabets
  4. Reversed Semi-pyramid of * and numbers
  5. Full pyramid of *
  6. Full pyramid of numbers
  7. Reversed full pyramid of *
  8. Pascal's triangle
  9. Floyd's triangle

These are some of the basic and frequently created patterns. We can create many more different types of patterns and arts using programming languages.

1. Semi pyramid of *

Code:

Understanding:

We take the number of rows needed as input from the user. We take a "for loop" and nest another loop in it. The outer loop manages the positions or rows of the *s' to be inserted and the inner loop inserts the *s in their positions managing the columns. Suppose user gives the input as 3. It has to be like:

For i=1, j =1

For i=2, j=1,j=2

For j=3, j=1,j=2,j=3

Pattern Programs in C

2. Semi-pyramid of numbers

This is so similar to the half pyramid of stars. But, the only difference is that the stars have to be replaced with numbers in following way:

Code:

Understanding:

If you observe clearly, the numbers that are to be printed are the values of j. For a clear understanding, suppose we give r = 4,

In the first iteration: i=1, j=1

In the second iteration: i=2,j=1,j=2

In the third: i=3,j=1,j=2,j=3

In the fourth: i=4,j=1,j=2,j=3,j=4

So, we print the values of "j" in every iteration.

3. Semi-pyramid of Alphabets:

Again, it's the same but now, we have to print alphabets like:

Code:

Understanding:

Here, we need to print the nth alphabet in the nth row, n number of times. Rather than asking the number of rows wanted, we can use this relationship. We ask the user what is the alphabet that has to be printed in the last row. According to the number of that alphabet, we'll know the number of rows needed. Suppose, user wanted E, E is 5th alphabet. So:

A

B B

C C C

D D D D

E E E E E

Points to be noted:

  • Suppose input is E:
    input-'A' = 5
  • ++alpha ='A' + 1 = 'B'
    'B' + 1 = 'C'….

4. Reversed Semi-pyramid of *

Analyzing the difference, it is the arrangement. i and j are responsible for arrangement, so we need to change them.

It has to look like:

Code:

Understanding:

Here, the row value we have to print has to be reversed, 1 has to become 5, 2-4, 3-3, 2-4, 1-5.

And for that i has to be iterated from the last or end and rather than incrementing, we have to decrement it.

Hence, we started i from the value of rows and decremented it till 1.

5. Reversed Semi-pyramid of numbers

It's the same as the Semi-pyramid of numbers but has to be reversed-follow the "i" iteration from the above pyramid:

Code:

FULL PYRAMIDS

6. Full pyramid of *

Output:

         *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *

Code:

7. Full pyramid of numbers:

Output:

         1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

Code:

8. Inverted full pyramid of *

Output:

* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *

Code:

9. Pascal's Triangle

Output:

           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1

Code:

10. Floyd's Triangle

Output:

1
2 3
4 5 6
7 8 9 10

Code:







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA