dimanche 22 décembre 2019

what is wrong with the logic that i am using?

theres a logic error , for the code i m trying to do ,

  *
 **
***
 **
  *

so this is the pattern , what my code is running is this

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

there seems to be logic error for the printing of stars , i just wanted to know what logic should i use. heres the code :-

#include<iostream>
using namespace std;
int main()
{

    /*

       *
      **
     ***
      **
       *

    */
    int i,rows;
    cout<<"Enter number of rows :"<<endl;
    cin>>rows;
    for(i = 1; i <= ((rows/2)+1) ; i++)
    {
        for(int j = (rows  - i); j >= 1; j--)
        {
            cout<<" ";
        }
        for(int k = 1; k <= i; k++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
    for(i = ((rows/2)+1) ; i <= rows; i++)
    {
        for(int j = 1; j <= i; j++)
        {
            cout<<" ";
        }
        for(int k = (rows/2); k >= 1; k--)
        {
            cout<<"*";
        }
        cout<<endl;
    }


    return 0;
}

Aucun commentaire:

Enregistrer un commentaire