jeudi 12 août 2021

Better way to print from the user inputted in rows and columns? Without for loops? Recursion?

I have been trying to search if there are other ways to print the below pattern:

Print a solid Rectange

 ####
 ####
 ####

I used the below code:

using namespace std;

int main ()
{
    int r,c;
    cout<<"Enter the number of rows and then columns:\n";
    cin>>r>>c;
    cout<<"Pattern:\n";
    for(int i=1; i<=r; i++)
    {
        for(int j=1; j<=c; j++)
        {
            cout<<"*";
        }
    cout<<"\n";
    }
    return 0;
}

My main question is that is there any way to remove the nested for loop or any way to make this code better?

Can we do a recursion on it? Is there a base case?

Aucun commentaire:

Enregistrer un commentaire