C Program to print Pyramid by star or numbers

C Programming C Programming Code Example Programming

C Program to print Pyramid by star or numbers

First Star printing Problem in C:

Problem 1: Write the the following star program to print the stars in C language:

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

Problem analysis:

  1. First take a variable of rows number to print how many number of rows.
  2. Make a for loop upto that the row number
  3. Look the number of star is incrementing in every line, so add another for loop and by checking if it is less than the previous rows or not and then print a star
  4. Thus if loop run 5 times, then it will print 5 rows with the above format. For better clear, see the code first.

Star printing problem solution in C language:

#include <stdio.h>
int main()
{

int
number, c, k;

printf("Enter number of rowsn");
scanf("%d",&number);

for
( c = 1 ; c <= number ; c++ )
{

for
( k = 1 ; k <= c ; k++ )
{

printf("*");
}

printf("n");
}


return
0;
}


Output with code view of this pyramid or star printing problem in C:

C Program to print star - Pyramid

Problem 2 

Write a C program which will create a pyramid like the below with C programming/ Create a left side/ right side star printing program in C language.

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

Star printing in C – problem code 2:


#include <stdio.h>

int
main()
{

int
row, c, n, temp;

printf("Enter the number of rows in pyramid of stars you wish to see ");
scanf("%d",&n);

temp = n;

for
( row = 1 ; row <= n ; row++ )
{

for
( c = 1 ; c < temp ; c++ )
printf(" ");

temp--;

for
( c = 1 ; c <= 2*row - 1 ; c++ )
printf("*");

printf("n");
}


return
0;
}

Problem 3 

Write a C program to print half pyramid using numbers like this-

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


Simple C code implementation of printing half pyramid using numbers Solution:

#include <stdio.h>
int main()
{

int
i, j, rows;

printf("Enter number of rows: ");
scanf("%d",&rows);

for
(i=1; i<=rows; ++i)
{

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

printf("%d ",j);
}

printf("n");
}

return
0;
}

Problem 3:

C Program to print pyramid using numbers like this.


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:

#include <stdio.h>
int main()
{
int i, space, rows, k=0, count = 0, count1 = 0;

printf
("Enter number of rows: ");
scanf
("%d",&rows);

for(i=1; i<=rows; ++i)
{
for(space=1; space <= rows-i; ++space)
{
printf
(" ");
++count;
}

while(k != 2*i-1)
{
if (count <= rows-1)
{
printf
("%d ", i+k);
++count;
}
else
{
++count1;
printf
("%d ", (i+k-2*count1));
}
++k;
}
count1
= count = k = 0;

printf
("n");
}
return 0;
}

Conclusion:

In this tutorial, we’ve learnt the following code-

  1. How to make a star pyramid in C language
  2. How to print a half star pyramid using C language
  3. How to print a pyramid with space and in the middle
Having any problems in those codes, just comment below and stay with us to learn more C code example.

Leave a Reply

Your email address will not be published. Required fields are marked *