Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2017-01-11 04:34:09

Zeeshan 01
Member
Registered: 2016-07-22
Posts: 650

Complex Loop

Any one can print this
*******   *******
******  *  ******
*****    *   *****   
****  *  *  *  ****
***        *     ***
**          *      **
*                     *

Last edited by Zeeshan 01 (Yesterday 15:30:53)


MZk

Offline

#2 2017-01-11 23:36:32

iamaditya
Member
From: Planet Mars
Registered: 2016-11-15
Posts: 766

Re: Complex Loop

In which programming language do you want the loop program to be written?


Practice makes a man perfect.
There is no substitute to hard work
All of us do not have equal talents but everybody has equal oppurtunities to build their talents.-APJ Abdul Kalam

Offline

#3 2017-01-12 04:40:02

Zeeshan 01
Member
Registered: 2016-07-22
Posts: 650

Re: Complex Loop

iamaditya wrote:

In which programming language do you want the loop program to be written?


I prefer Java.

Last edited by Zeeshan 01 (Yesterday 15:34:03)


MZk

Offline

#4 2017-01-12 18:28:23

phrontister
Real Member
From: The Land of Tomorrow
Registered: 2009-07-12
Posts: 4,600

Re: Complex Loop

Hi;

I don't know c or cpp, but here's a code in BASIC that will do it...

    a$="*******":b$="                     ":c=7:d=1
    While c>0
        Print Right$(a$,c)+Right$(b$,d*3)+Right$(a$,c)
        c=c-1:d=d+1
    WEnd

It runs in LibertyBASIC (paid software) and JustBASIC (freeware), but in most other BASIC programs the code will need some tweaking to work.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." - Ted Nelson

Offline

#5 2017-01-12 20:50:49

phrontister
Real Member
From: The Land of Tomorrow
Registered: 2009-07-12
Posts: 4,600

Re: Complex Loop

No, b$ isn't empty: it contains 21 blank spaces.

Each of the seven lines of printing in your first post contains two equal groups of asterisks that are separated by a group of blank spaces. Line 1 has 3 blank spaces, increasing by 3 in each subsequent line until reaching 21 blank spaces in the last (7th) line.

Right$(b$,d*3) for line 1 selects the right-most 3 blank spaces from b$, and, using the increasing value of d, for each subsequent loop selects 3 more spaces from b$ than for the previous line.

Right$(a$,c) for line 1 selects the right-most 7 asterisks from a$, and, using the reducing value of c, for each subsequent loop selects 1 less asterisk from a$ than for the previous line.

Btw, here is the output from my program: 

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

Last edited by phrontister (2017-01-12 21:24:49)


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." - Ted Nelson

Offline

#6 2017-01-12 20:57:05

phrontister
Real Member
From: The Land of Tomorrow
Registered: 2009-07-12
Posts: 4,600

Re: Complex Loop

Here's a different method...

    a=8:b=0
    While a>1
        a=a-1:b=b+3
        For aa=1 To a
            Print "*";
        Next aa
        For bb=1 To b
            Print " ";
        Next bb
        For aa=1 To a
            Print "*";
        Next aa
        Print
    WEnd

And the output:

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

Last edited by phrontister (2017-01-12 21:00:48)


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." - Ted Nelson

Offline

Board footer

Powered by FluxBB