Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Write a C program to print right triangle star(*) pattern series using for loop.

How to print right triangle star


pattern series of n rows using for loop in C programming. The pattern with 5 rows should look like:
*
**
***
****
*****

Required knowledge:
Basic C programming, For loop

Logic:
Printing right triangle pattern is simple if you got the pattern. If you look to the pattern carefully you will find
that you have to print stars in increasing order of rows (i.e. 1 star in first row, followed by 2 stars in second and
so on...). To do so we will use the concept of square star pattern with a little change in inner loop code. In the
pattern given above the total number of stars(columns) in each row is equal to the row number and we will use
this as a termination limit in the inner for loop.

Program:

1 /*
2 * C program to print right triangle star pattern series
3 */
4
5 #include <stdio.h>
6
7 int main()
8 {
9 int i, j, n;
10
11 //Reads the number of rows to be printed from user
12 printf("Enter value of n: ");
13 scanf("%d", &n);
14
15 for(i=1; i<=n; i++)
16 {
17 //Print i number of stars
18 for(j=1; j<=i; j++)
19 {
20 printf("*");
21 }
22
23 //Move to next line/row
24 printf("\n");
25 }
26
27 return 0;
28 }

Write a C program to print hollow right triangle star(*) pattern series using for loop. How to print hollow right
triangle star pattern series of n rows using for loop in C programming. The pattern with 5 rows should look like:
*
**
**
* *
*****
Logic:
If you have printed right triangle and hollow square star patterns before then this wouldn't trouble much. If you
look to the given pattern carefully you will find that stars are printed only when row=1 or column=1 or
column=n (where n is the total number of rows). For this what you need to do is make little change in the right
triangle star pattern series i.e. we must check a condition mentioned before printing star(*).

Program:

1 /**
2 * C program to print hollow right triangle star pattern
3 */
4
5 #include <stdio.h>
6
7 int main()
8 {
9 int i, j, n;
10
11 //Reads number of rows to be printed from user
12 printf("Enter number of rows: ");
13 scanf("%d", &n);
14
15 for(i=1; i<=n; i++)
16 {
17 for(j=1; j<=i; j++)
18 {
19 if(j==1 || j==i || i==n)
20 {
21 printf("*");
22 }
23 else
24 {
25 printf(" ");
26 }
27 }
28
29 printf("\n");
30 }
31
32 return 0;
33 }
Write a C program to print mirrored right triangle star(*) pattern series using for loop. How to print right
triangle star pattern series with trailing spaces of n rows using for loop in C programming. The pattern with 5
rows should look like:
*
**
***
****
*****

Logic:
If you have gone though the previous post to print the right triangle star pattern then printing this wouldn't be
difficult. What you need is to add an extra inner loop that will print spaces before * gets printed. And if you
carefully look to the spaces you will find a special pattern of spaces that are eventually in decreasing order of
row (i.e. first row contains n-1=4 spaces, followed by 3 and so on... Where n is the total number of rows).
Program:
1 /**
2 * C program to print mirrored right triangle star pattern series
3 */
4
5 #include <stdio.h>
6
7 int main()
8 {
9 int i, j, n;
10
11 //Read number of rows to be printed from user
12 printf("Enter value of n: ");
13 scanf("%d", &n);
14
15 for(i=1; i<=n; i++)
16 {
17 //Used for printing spaces in decreasing order of row
18 for(j=i; j<n; j++)
19 {
20 printf(" ");
21 }
22
23 //Prints star in increasing order or row
24 for(j=1; j<=i; j++)
25 {
26 printf("*");
27 }
28
29 printf("\n");
30 }
31
32 return 0;
33 }

Write a C program to print reverse/inverted right triangle star(*) pattern series using for loop. How to print
reverse/inverted right triangle star pattern series of n rows using for loop in C programming. The pattern with 5
row should look like:
*****
****
***
**
*

Program:

1 /**
2 * Reverse right triangle star pattern program in C
3 */
4
5 #include <stdio.h>
6
7 int main()
8 {
9 int i, j, n;
10
11 //Reads number of rows to be printed from user
12 printf("Enter value of n : ");
13 scanf("%d", &n);
14
15 for(i=1; i<=n; i++)
16 {
17 for(j=i; j<=n; j++)
18 {
19 printf("*");
20 }
21
22 //Moves to the next line/row
23 printf("\n");
24 }
25
26 return 0;
27 }
Write a C program to print the equilateral triangle or Pyramid star(*) pattern series of n rows using for loop.
How to print Pyramid star pattern series using for loop in C programming. The pattern of 5 rows should look
like:
*
***
*****
*******
*********

Logic:
There can be many ways of thinking of the problem here I am discussing the easiest one. Here we need to print
two things one is the trialing spaces and other is the equilateral triangle. As like other star pattern problems here
also spaces are arranged in a special way i.e. each row contains n - row_number spaces (where n is the total
number of rows).

When you look to the total number of stars(*) in the equilateral triangle pattern you will notice that each row
contains 2*rownumber - 1 stars.

Program:

/**
* C program to print equilateral triangle or pyramid star pattern
1
*/
2
3
#include <stdio.h>
4
5
int main()
6
{
7
int i, j, n;
8
9
//Reads number of rows to be printed
10
printf("Enter value of n : ");
11
scanf("%d", &n);
12
13
for(i=1; i<=n; i++)
14
{
15
//Prints trailing spaces
16
for(j=i; j<n; j++)
17
{
18
printf(" ");
19
}
20
21
//Prints the pyramid pattern
22
for(j=1; j<=(2*i-1); j++)
23
{
24
printf("*");
25
}
26
27
printf("\n");
28
}
29
30
return 0;
}

You might also like