lundi 25 juin 2018

How can I solve the following pattern in c++?

The following pattern has to be made:

1 2 3 4 5

1 2 3 4 * 

1 2 3 * * *

1 2 * * * * *

1 * * * * * * *

Here is the code that I've written. It works fine for n=5 but displays incorrect results for n>5:

#include <iostream>
using namespace std;
int main(){
int n,star,k=0;; cin>>n;
for(int row=n;row>0;row--)
{

for(int i=1;i<=row;i++)
{
    cout<<i<<" ";
}
if(row>3){star=n-row;}
else if(row<=3){star=n-row+k-1;}k++;
for(int i=0;i<star;i++)
{
cout<<"*"<<" ";
}
cout<<endl;
}}

Please explain what can be done.

Aucun commentaire:

Enregistrer un commentaire