#include <iostream>
#include <conio.h>

using namespace std;
int counter=1;
int m=1;
int n,j,o;

int main()
{
    for (n=9;n>=1;n--)
    {
        for (j=5;j>=counter;j--)
        {
        cout<<"*";
        }


        if(counter>5)

           {
               for (o=0;o<=m;o++)

               {
                   cout<<"*";
               }
               m++;
           }
        counter++;
    cout<<endl;
    }

    getch();
}

my problem is, i need to enter number to get that output, example enter no: 5 it will give me that output, please help me! thanks

Edited 3 Years Ago by Dani: Formatting fixed

Attachments Untitled.jpg 18.03 KB

The number of lines/rows in your output is 2*n - 1 where n is the number you enter. For ex. if you enter 5 , you will have 9 rows, if you enter 3, you will have 5 rows.
Hence, your outer for loop should have a condition that is related to the number of lines that will be printed. Your inner for loop will decide how many stars to print depending on which line it is (that is nothing but index of your outer for loop.)

And format your code like this, so that it becomes easy for you to understand and debug

#include <iostream>
#include <conio.h>

using namespace std;
int counter=1;
int m=1;
int n,j,o;

int main()
{
    for (n=9;n>=1;n--)
    {
       for (j=5;j>=counter;j--)
       {
          cout<<"*";
       }

       if(counter>5)
       {
          for (o=0;o<=m;o++)
          {
             cout<<"*";
          }
          m++;
       }

       counter++;
       cout<<endl;
   }

   getch();
}

Also another problem I notice in your code is, even though counter > 5 , inner for loop of j will still be executed. But you don't want that to happen. You only want for loop with index o to run now. So , you need to use else statement here.
You can do,

if(counter < 5)
 {
    //your first inner for loop with j index
 } 
 else
 {
  //your second inner for loop with index o
 }

The number of lines/rows in your output is 2*n - 1 where n is the number you enter. For ex. if you enter 5 , you will have 9 rows, if you enter 3, you will have 5 rows.
Hence, your outer for loop should have a condition that is related to the number of lines that will be printed. Your inner for loop will decide how many stars to print depending on which line it is (that is nothing but index of your outer for loop.)

can u make for me the code? because i dont really know, my head aches :( if not, thanks btw

You already have done most of the code. You only need to make small changes to make it generic.
Take input from user in ,say , n. Your 9 (number of rows will be replaced by 2*n - 1).
Your j will now start from n (since it is the number of stars you want to print on the first line) and again instead of checking counter against 5 you will check it against n. Give it a try. Post the code back if there is a problem. :)

You already have done most of the code. You only need to make small changes to make it generic.
Take input from user in ,say , n. Your 9 (number of rows will be replaced by 2*n - 1).
Your j will now start from n (since it is the number of stars you want to print on the first line) and again instead of checking counter against 5 you will check it against n. Give it a try. Post the code back if there is a problem. :)

ive already tried but theres so many error, i dunno what to do, im so sleepy, i will get 0 grade for my home work :( hahaha thanks btw! im just a beginner :P

This article has been dead for over six months. Start a new discussion instead.