looping

how to create a program in turbo c++ that the output would be an asterisks running in the 4 sides of the screen?
you mean something like this?
1
2
3
4
5
6
7
8
9
 *       *
  *     *
   *   *
    * *
     *
    * *
   *   *
  *     *
 *       *


You need to figure out that such asterisk (or any other figure, including christmass tree - that was my assignment few years ago) consist of "*" and SPACES. All we have to do now is to operate with these *'s and spaces correctly :-)

Here we go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//if you prefer stdio, just change the cout/cin for printf/scanf etc
#include <iostream> 
// you probably don't need this line below
using namespace std; 

int main()
{
    int size;
    cout<<"Enter asterisk's size: "; 
    cin>>size; //asterisk will be slightly different depending on number (even/odd) you enter

    for(int i=0;i<size;i++)
    {
        for(int j=0;j<size;j++)
        {
            if(j==i || j==(size-1)-i)   //we print the *s aslant
            {
                if(j==(size-1))
		    cout<<"*"<<"\n"; //last "column" of the figure
                else 
		    cout<<"*";
            }
            else if(j==(size-1)) 
		cout<<" "<<"\n"; //last "column" of the figure
            else 
		cout<<" ";
        }
    }

    return 0;

}
Last edited on
Topic archived. No new replies allowed.