Pattern Programs In C

  • Semi-pyramid of *
  • Semi-pyramid of numbers
  • Semi-pyramid of alphabets
  • Reversed Semi-pyramid of * and numbers
  • Full pyramid of *
  • Full pyramid of numbers
  • Reversed full pyramid of *
  • Pascal's triangle
  • Floyd's triangle

These are a few fundamental and commonly generated patterns. It is possible to generate a variety of diverse patterns and designs using programming languages.

1. Semi pyramid of *

Example

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

Code:

Example

#include <stdio.h>
int main() 
{
   int i, j, r;
   printf("Input the number of rows: ");
   scanf("%d", &r);
   for (i = 1; i <= r; i++)
	{
   		   for (j = 1; j <= i; j++)
		   {
       			  printf("* ");
    		   }
    		  printf("\n");
  	 }
   return 0;
}

Understanding:

We request the user to input the desired number of rows. Utilizing a "for loop," we proceed to nest another loop within it. The primary loop controls the placement of the asterisks in each row, while the secondary loop handles the insertion of asterisks in their respective columns. For instance, if the user specifies 3 as the input, the output pattern will resemble:

Example

*
**
***

For i=1, j =1

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

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

2. Semi-pyramid of numbers

This pattern closely resembles the half pyramid of stars. However, the key distinction here is that instead of using stars, we are utilizing numbers in the specified manner:

Example

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Code:

Example

#include <stdio.h>
int main()
{
   int i, j, r;
   printf("Enter the number of rows: ");
   scanf("%d", &r);
   for (i = 1; i <= r; i++) 
   {
    	  for (j = 1; j <= i; j++)
	   {
       		  printf("%d ", j);
	   }
   printf("\n");
   }
   return 0;
}

Understanding:

If you carefully examine the scenario, the numbers to be displayed correspond to the values of j. To illustrate better, let's assume r = 4, ```

*

  • *
  • *
  • Example
    
    
    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 display the values of "j" during each iteration.
    
    ### 3. Semi-pyramid of Alphabets:
    
    Again, we are required to display alphabets in the following manner:
    
    

A

B B

C C C

D D D D

E E E E E

Example


Code:

include <stdio.h>

int main

{

int i, j;

char last_al, alpha = 'A';

printf("Enter an uppercase character to be in the in the last row: ");

scanf("%c", &last_al);

for (i = 1; i <= (last_al - 'A' + 1); i++)

{

for (j = 1; j <= i; j++)

{

printf("%c ", alpha);

}

++alpha;

printf("\n");

}

return 0;

}

Example


Understanding:

Here, our objective is to display the nth letter in the nth row, n times. Instead of requesting the desired number of rows, we can rely on this correlation. We prompt the user to specify the alphabet they wish to see in the final row. By determining the position of that alphabet, we can ascertain the requisite number of rows. For instance, if the user selects E, which is the 5th letter in the alphabet, then:

C C C

D D D D

E E E E E

Points to consider:

- For instance, if the input is E: input-'A' = 5

- Increment alpha ='A' + 1 = 'B' 'B' + 1 = 'C'….

### 4. Reversed Semi-pyramid of *

When examining the variance, the key factor is the organization. The variables i and j play a crucial role in determining the arrangement, hence requiring us to modify them.

It has to look like:

  • *
  • *

*

Example


Code:

include <stdio.h>

int main

{

int i, j, r;

printf("Enter the number of rows: ");

scanf("%d", &r);

for (i = r; i >= 1; i--)

{

for (j = 1; j <= i; j++)

{

printf("* ");

}

printf("\n");

}

return 0;

}

Example


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 to achieve that, it is necessary to iterate starting from the final position and instead of increasing, we should decrease it.

Therefore, we initiated i with the rows value and decreased it until reaching 1.

### 5. Reversed Semi-pyramid of numbers

It follows a similar pattern to the Semi-pyramid of numbers, but in this case, the iteration "i" is reversed compared to the pyramid above.

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

Example


Code:

include <stdio.h>

int main

{

int i, j, r;

printf("Enter the number of rows: ");

scanf("%d", &r);

for (i = r; i >= 1; i--)

{

for (j = 1; j <= i; j++)

{

printf("%d ", j);

}

printf("\n");

}

return 0;

}

Example


## FULL PYRAMIDS

### 6. Full pyramid of *

Output:

*

  • Example
    
    
    Code:
    
    

    include <stdio.h>

int main

{

int i, space, r, k = 0;

printf("Enter the number of rows: ");

scanf("%d", &r);

for (i = 1; i <= r; ++i, k = 0)

{

for (space = 1; space <= r - i; ++space)

{

printf(" ");

}

while (k != 2 * i - 1)

{

printf("* ");

++k;

}

printf("\n");

}

return 0;

}

Example


### 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

Example


Code:

include <stdio.h>

int main

{

int i, space, r, k = 0, c = 0, c1 = 0;

printf("Enter the number of r: ");

scanf("%d", &r);

for (i = 1; i <= r; ++i)

{

for (space = 1; space <= r - i; ++space)

{

printf(" ");

++c;

}

while (k != 2 * i - 1)

{

if (c <= r - 1) {

printf("%d ", i + k);

++c;

}

else

{

++c1;

printf("%d ", (i + k - 2 * c1));

}

++k;

}

c1 = c = k = 0;

printf("\n");

}

return 0;

}

Example


### 8. Inverted full pyramid of *

Output:

*

Example


Code:

include <stdio.h>

int main

{

int r, i, j, space;

printf("Enter the number of r: ");

scanf("%d", &r);

for (i = r; i >= 1; --i)

{

for (space = 0; space < r - i; ++space)

printf(" ");

for (j = i; j <= 2 * i - 1; ++j)

printf("* ");

for (j = 0; j < i - 1; ++j)

printf("* ");

printf("\n");

}

return 0;

}

Example


### 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

Example


Code:

include <stdio.h>

int main

{

int r, coef = 1, space, i, j;

printf("Enter the number of r: ");

scanf("%d", &r);

for (i = 0; i < r; i++)

{

for (space = 1; space <= r - i; space++)

printf(" ");

for (j = 0; j <= i; j++)

{

if (j == 0 || i == 0)

coef = 1;

else

coef = coef * (i - j + 1) / j;

printf("%4d", coef);

}

printf("\n");

}

return 0;

}

Example


### 10. Floyd's Triangle

Output:

1

2 3

4 5 6

7 8 9 10

Example


Code:

include <stdio.h>

int main

{

int r, i, j, number = 1;

printf("Enter the number of r: ");

scanf("%d", &r);

for (i = 1; i <= r; i++)

{

for (j = 1; j <= i; ++j)

{

printf("%d ", number);

++number;

}

printf("\n");

}

return 0;

}

Example


Input Required

This code uses input(). Please provide values below: