Shapes by Nested for loop

Can anyone tell me how to sketch shapes like square with empty space in a middle, diamond with empty space and without empty space, too, an arrow, separately, pointing all directions, an oval shape empty space and without empty, and all other by using nested for loop? Kindly guide me with its coding.
Program for square with empty space within


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    int j,i,n;
    cout<<"Enter the number of lines : ";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(j==1 || j==n || i==1 || i==n)
                cout<<'A';
            else
                cout<<' ';
        }
        cout<<endl;
    }
    cout<<endl;
    system("PAUSE");
}


i controls the line .for each line we have some characters that is controlled by j. the condition for square is
1) in first and last line print the character in all places.
for this i specify the condition if(i==1 || i==n)
2) in all other lines we have to put the character in first and last place only
for this i specify the condition if(j==1 || j==n)
3) for all lines between first and last we have to put space in the placed left by second condition.
for this i specify the condition else cout<<' ';


enjoy programming :-)
Last edited on
Topic archived. No new replies allowed.