mardi 4 mai 2021

Is there any way to print following pattern without using nested loops?

This image contains a pattern. The '*' is getting subtracted and '-' being added.

Where I am confused that I am not allowed to use nested loop. I don't know why it confused me.

#include <iostream>
using namespace std;
int main(){
    string a="*";
    for (int i=0; i<4; i++){
        cout<<a<<endl;
        a=a+"*";
    }
}

I tried this code to test to print the pattern which successfully it did. Following was the pattern made by it.

*
**
***
****

This seems fine because C++ allowed a=a+"*" which was addition of string into string data. But the pattern I require is

****
***-
**--
*---

In it, the '-' can be added but how can I subtract '*'. I can't do a=a-'*'. Do I need to use indexes of the string? But only one loop is allowed.

Aucun commentaire:

Enregistrer un commentaire