Program to print pyramid of stars

4 replies [Last post]
pkpattnaik
Offline
Last seen: 10 years 17 weeks ago
Joined: 2009-12-04
bOt Points: 62

Hey all,

 

I  have tried to proint pyramid of stars, my logic goes like below. I am not getting the correct output. Can anyone help me to solve this.

 

 

for ($i=1;$i<=5;$i++)
{

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

print "*";

}

print "\n"; 

 

Thanks in advance

 

Prashant

shashwat
Offline
Last seen: 1 year 31 weeks ago
Joined: 2009-02-18
bOt Points: 1160
Hello pkpattnaik, Check out

Hello pkpattnaik,

Check out this program -

 

#include <iostream>

using namespace std;


int main()

{

for (int i=1;i<=5;i++)

{


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

{

cout<<"*";

}

cout<< "\n"; 

}

}

Founder and Admin

pkpattnaik
Offline
Last seen: 10 years 17 weeks ago
Joined: 2009-12-04
bOt Points: 62
See Here

The output should look like this:

 

  *  

 * * 

* * *

 

 

shashwat
Offline
Last seen: 1 year 31 weeks ago
Joined: 2009-02-18
bOt Points: 1160
Check out this code -

Check out this code -  

 

#include<iostream.h>

int main()

{

//creating a pyramid in c++

for (int i=1; i<=8;i++)

{

for(int j=3*(8-i)/2;j>=1;j--) 

cout<<" ";

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

cout<<"*";

cout<<endl;

}

return 0;

}

 

Founder and Admin

pkpattnaik
Offline
Last seen: 10 years 17 weeks ago
Joined: 2009-12-04
bOt Points: 62
A Better Ordering

Thanks a lot Shashwat, still we can arrange the *'s to look a better pyramid structure. Here is my observation goes:

 

#include <stdio.h>

int main()

{

    int i, k, m, n= 1;

    scanf("%d",&n);

    for(m=0;m<2;m++)

    {

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

          {

               if(m==0){

                    for(k = 1; k<=n-i; k++)

                    {

                         printf("  ");

                    }

                    for(k=1;k<2*i;k++){

                         printf("%s","*");

                    }

               }

               printf("\n");

          }

     }

  return 0;

}

 

 

Thanks again...