C Tutorial – A Star pyramid and String triangle using for loops
In this C language tutorial we will look at a much-requested subject, printing a star pyramid and a string triangle using for loops.
We know that this question is often asked as part of homework (lab) assignments, but we got so much requests that we couldn’t ignore it. BUT we are urging you to at least try to make the homework questions yourself! At the end of this tutorial we will give two alternative questions, so you can try to fix those yourself.
Star Pyramid in C
In this section we will look how to make a Star pyramid using three “for loops”. Take a look at the following source code example:
#include <stdio.h>
int main() {
int rows, star, spaces;
int number_of_stars = 6;
int number_of_rows = number_of_stars;
for (rows=1; rows <= number_of_rows; rows++) {
for (spaces=1; spaces <= number_of_stars; spaces++) {
printf(" ");
}
for (star=1; star <= rows; star++) {
printf("*");
printf(" ");
}
printf("\n");
number_of_stars = number_of_stars - 1;
}
return 0;
}
First we declare some variables, such as number_of_stars (the number of stars at the base of the pyramid) and number_of_rows (the height of the pyramid, in this case the same as the number_of_ stars). We first setup a ‘for loop’ (learn more about for loops here) so our pyramid is divided in rows.
In the second ‘for loop’ a certain number of spaces before a star. This way we can place a star at a certain point in each row. The actual placing of the star is done in the third ‘for loop’.
The third ‘for loop’ will print the number of stars that is needed for a certain row.
After we end a row with a newline escape sequence character we just have to lower the number_of_stars by one.
That is all there is to it! The output will look like this:
*
* *
* * *
* * * *
* * * * *
* * * * * *
String Triangle in C
In the second source code example in this C language tutorial we will look at how to create a string triangle. Here is example:
#include <stdio.h>
#include <string why not check here.h>
int main() {
int i,c,length;
char arr[] = "Hello Everybody!";
length = strlen(arr);
for (i = length; i >= 0; i--) {
printf("\n");
for (c = 0; c < i; c++) {
printf("%c",arr[c]);
}
}
return 0;
}
We start out by declaring some integers and an array of characters that is initialized with the string “Hello Everybody!”. After this is done, the length of the number of characters is determined and stored in the variable ‘length’.
This variable is then used in the first ‘for loop’. This ‘for loop’ loops until the last character is printed on the screen (by looping length times).
In the second ‘for loop’ the actual printing is done. In this loop the number of characters, determined by integer i, are printed. Because integer i is decreased in the first ‘for loop’, the number of characters printed will also decrease with each loop run.
The output will look like this:
Hello Everybody!
Hello Everybody
Hello Everybod
Hello Everybo
Hello Everyb
Hello Every
Hello Ever
Hello Eve
Hello Ev
Hello E
Hello
Hello
Hell
Hel
He
H
That is all there is to it.
Alternative Questions
Now that you’ve seen the two examples, it’s wise to make your own programs. Of course you can think up your own exercise, but we have also two for you: recreate the two previous source code example and make the result face the opposite position. So you get an upside down star pyramid and a string triangle with the point facing up.
The results of these exercise can be found here, but at least try them yourself first (that’s the only way to learn).
That’s that for this C tutorial, until the next C tutorial.
thanks..,,,i really helped me…. in my assignment.
thanks a lot for this…it help me a lot
thanks its a very nice example to learn c but i want some more programs………….
@Preeti : also take a look at the C programming Algorithms for more program examples. And in the future there is more to come, so check back later!
thanks… its helpful….
thank u very much sir!!!!!you are good teacher for beginners of programming
its good trick to solve this problem
thanks its a very nice example
Nice
Very Nice
Thank you so much. You provided me with a better understanding of basic programming concepts.
nice
Sir i have done first task self….which is as below…
#include
#include
int main(void)
{
int rows, star, spaces;
int number_of_stars = 6;
int number_of_rows = number_of_stars;
for (rows=1; rows <= number_of_rows; rows++) {
for (spaces=1; spaces = rows; star–) {
printf(“*”);
printf(” “);
}
printf(“\n”);
number_of_stars = number_of_stars + 1;
}
getch();
return 0;
}
Just three for loops and three pyramids . Have Fun. GaRUi
/* DH GaRUi */
#include
#include
#define pyramid_floor 11
#define pyramid_top 1
#define pyramid_increase 2
int main()
{
int i;
for (i=pyramid_top; i<=pyramid_floor ; i+=pyramid_increase)
{
cout << setw(pyramid_floor-i/pyramid_increase)<< setfill(' ') << ' ' << setw(i) << setfill('*') << '*' << endl;
}
cout << endl;
int j;
for (i=pyramid_top, j=pyramid_floor/pyramid_increase; i<=pyramid_floor ; i+=pyramid_increase, –j)
{
if(i<pyramid_floor ) { cout << setw(j)<< setfill(' ') << ' ';}
cout << setw(i) << setfill('*') << '*' << endl;
}
cout <=pyramid_top ; i-=pyramid_increase, ++j)
{
if(i<pyramid_floor ) { cout << setw(j)<< setfill(' ') << ' ';}
cout << setw(i) << setfill('*') << '*' << endl;
}
return 0;
}
#include iostream and iomanip line deleted on posting. I think this post mechanism use 0x3E and/or 0x3C ASCII codes for some redirection.
it is good you did good job.
Thanks,i had need of it
thankyou this program is helpful to me
the code for the pyramid is a bit confusing because you are subtracting the number of spaces each time you move to a next row but the variable is number_of_stars…thanks for the code you use a 1..2..3..4 pattern(with space after each star) and i learned the 1..3..5..7 pattern recently so at least i can know two ways of solving this problem you should do more pattern tutorials
as a beginner the diamond shape tutorial is a bit confusing
hai..!
i want a program to print a given string in traingle(or pyramid) form by using only one for loop.
can anyone do it for me??
why do we lower the number of stars by 1 in the end (in the star pyramid)? please explain. thanx in advance:}
hai,how to draw cirle in c,langauge can anybody help me>
hai,how to print encoding and decoding programs in c( use 2 dimentional array)language
#include
#include
int main() {
int i,c,length;
char arr[] = “Hello Everybody!”;
length = strlen(arr);
for (i = length; i >= 0; i–) {
printf(“\n”);
for (c = 0; c < i; c++) {
printf("%c",arr[c]);
}
}
return 0;
}
sir how can i print a circle in c language…if anyone master here in c language then contact me at malik_02394@yahoo.com
is it possible to make star pyramid with only 2 “for” ??
Program for finding the alternate digits of the number If number is 35846 then Sum 1=3+8+6=17 Sum 2=5+4=9
//check this out
#include
void main()
{
int i,j,s;
printf(“\n”);
for(i=0;ii;s–)
{
printf(” “);
}
for(j=0;j<=i;j++)
{
printf("1");
}
for(j=0;j<i;j++)
{
printf("1");
}
printf("\n");
}
for(i=0;i<=3;i++)
{
for(s=0;si;j–)
{
printf(“1”);
}
for(j=3;j>i;j–)
{
printf(“1”);
}
printf(“\n”);
}
return(0);
}
#include
#include
void main()
{
int i,j,l=0;
int k=1;
clrscr();
for (i=1;i=k;j–)
{
printf(“%d”,j);
}
k=k+i;
}
getch();