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: 746

Complex Loop

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

Last edited by Zeeshan 01 (2018-06-22 15:30:53)


Malik

Offline

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

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

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: 746

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 (2018-06-22 15:34:03)


Malik

Offline

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

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

Re: Complex Loop

Hi;

Zeeshan 01 wrote:

Any one can print this
*******   *******
******      ******
*****         *****   
****            ****
***               ***
**                  **
*                     *
Help me
By using loops

iamaditya wrote:

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


Zeeshan 01 wrote:

I prefer c or cpp....

For ,,while or do while !!!


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.

EDIT: Here's a slightly more streamlined version:

    a$="*******":b$="   ":c=7
    WHILE c>0
        PRINT RIGHT$(a$,c)+b$+RIGHT$(a$,c)
        c=c-1:b$=b$+"   "
    WEND

...and another variation:

    a$="*******":b$="   "
    WHILE LEN(a$)>0
        PRINT a$+b$+a$
        a$=RIGHT$(a$,LEN(a$)-1)
        b$=b$+"   "
    WEND

All three programs give the following output:

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

Last edited by phrontister (2018-11-22 23:01:47)


"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,884

Re: Complex Loop

Zeeshan 01 wrote:

b$="                     ":c=7
B is empty???

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.

EDIT: The following code explanation is for the first program in my previous post.

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.

Last edited by phrontister (2018-11-24 02:22:43)


"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,884

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 (2018-11-22 23:04:30)


"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

#7 2018-11-21 00:52:03

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

Re: Complex Loop

Hi Zeeshan 01;

Zeeshan 01 wrote:

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

I thought I'd give your new puzzle a go, but unlike with your original puzzle, a simple solution has me stumped this time because it seems to lack symmetry now that you've added that internal cross. dunno It's trickier now...hence your change of the thread title from "Help in simple prob of loop" to "Complex Loop", I suppose.

Zeeshan 01 wrote:

Thanks for help but I need c language code

I still don't know c, nor the other two programming languages you mentioned (cpp and Java); but anyway, in case it helps someone, here's my effort in BASIC, based on my first solution to your original puzzle:

EDIT: It's closer to the second solution, actually.

    a$="*******":c=7
    WHILE c>0
        IF c=7 THEN b$="   "
        IF c=6 THEN b$="  *  "
        IF c=5 THEN b$="    *   "
        IF c=4 THEN b$="  *  *  *  "
        IF c=3 THEN b$="        *     "
        IF c=2 THEN b$="          *      "
        IF c=1 THEN b$="                     "
        PRINT RIGHT$(a$,c)+b$+RIGHT$(a$,c)
        c=c-1
    WEND

My program's output:

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

Last edited by phrontister (2018-11-25 00:06:37)


"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