Site icon T4Tutorials.com

Write nested loops to print a rectangle. Sample output for given program

Write nested loops to print a rectangle. sample output for given program: * * * * * *.

A demo of the output for the given program is mentioned below;

* * *
* * *

C++ Program to Write nested loops to print a rectangle

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int StarRows,StarColumns,i,j;
    StarRows=2;
    StarColumns=3;
     //Takes input from user for StarColumns
     for(i=1; i<=StarRows; i++){//outer for loop
    for (j=1; j<=StarColumns; j++){//inner for loop
        cout<<"*";//print star
    }
    cout<<"\n";//move to next line
}
getch();
    return 0;
}

Output

* *  *

*  * *

C++ Program to Write nested loops to print a rectangle by asking the user

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int StarRows,StarColumns,i,j;
    cout<<"Please Enter the number of StarRows: ";
    cin>>StarRows;
    //Takes input from user for StarRows
    cout<<"Please Enter the number of StarColumns: ";
    cin>>StarColumns;
     //Takes input from user for StarColumns
     for(i=1; i<=StarRows; i++){//outer for loop
    for (j=1; j<=StarColumns; j++){//inner for loop
        cout<<"*";//print star
    }
    cout<<"\n";//move to next line
}
getch();
    return 0;
}

Output

Please Enter the number of StarRows:

2

Please Enter the number of StarColumns:

3

* *  *

*  * *

Exit mobile version