Sunday 23 September 2018

Printing a rectangular pattern of size i * j in C++

A rectangular pattern of symbols

The rectangular array of symbols is called a rectangular pattern. This pattern contains a certain number of rows and columns. It can contain any numbers, characters or symbols. A rectangular pattern with 4 rows and columns is given below:

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

Write a program in C++ to print a rectangular pattern of size i*j.


We have to input the number of rows and columns the user wants in the pattern. We will use a nested for loop in this program. The first loop will be executed for the number of rows. The second loop will be nested inside the first loop and will execute the number of columns to be printed in that row.

For loop is an iteration loop structure.

Syntax:
for(variable initialization; condition; variable increment or decrement)  
{
statement 1;
statement 2;
.
.
.
}

Program


#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,i,j;
clrscr();
cout<<"Enter the number of rows=";
cin>>a;
cout<<"Enter the number of columns=";
cin>>b;
cout<<"\n";
for (i=1;i<=a;i++)
{
for (j=1;j<=b;j++)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}



Output


Program to print rectangular pattern
PRINTING OF RECTANGULAR PATTERN









Explanation

In the program, the user wants 3 rows and 9 columns. So the outer loop will be executed 3 times (i.e. i=1 to 3). The inner nested loop will be executed 9 times(i.e. j=1 to 9). So we obtain a rectangular pattern of required size.



No comments:

Post a Comment