Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #26466

Re: PRINTING DIAMOND SHAPE WITH LOOPS!

From rossum <rossum48@coldmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: PRINTING DIAMOND SHAPE WITH LOOPS!
Date 2013-11-01 13:03 +0000
Message-ID <tr8779pscp80e5qr896kg4dudiu7evskrn@4ax.com> (permalink)
References <bd7dee67.0408241759.3884e4e9@posting.google.com> <4wTWc.18629$cx.18110@bignews4.bellsouth.net> <llWWc.228978$eM2.215971@attbi_s51> <ecaee893-0ad6-48a6-b345-24a33436b4df@googlegroups.com>

Show all headers | View raw


On Fri, 1 Nov 2013 01:30:23 -0700 (PDT), jyothi.m1089@gmail.com wrote:

>> class Test7
>> {
>>   public static final void main( String[] args )
>>   {

       // This line reads in integer, n, from the args array.
>>     int n = Integer.parseInt( args[ 0 ] );

       // Loop from 1 to n stepping 2 each time. [*1]
>>     for ( int i = 1; i <= n; i += 2 )
>>     {

         // This loop prints out the leading spaces needed for this
row.
>>       for ( int k = 0; k < ( n - i ); k += 2 )
>>         System.out.print( " " );

         // This loop prints out the correct number of stars for this
row of the diamond.
>>       for ( int j = 0; j < ( ( i * 2 ) - 1 ); j += 2 )
>>         System.out.print( "*" );

         // Mark the end of a single row of printing. [*2]
>>       System.out.println( "" );
>>     }

       // This part I will leave for you to examine yourself, with a
little help.
>>     for ( int i = 1; i <= n; i += 2 )
>>     {
>>       for ( int k = 1; k < i; k += 2 )
           // Print leading space
>>         System.out.print( " " );
>>       for ( int j = 0; j <= ( n - i ); j++ )
           // Print star.
>>         System.out.print( "*" );
         // End of this line.
>>       System.out.println( "" );
>>     }
>>   }
>> }
>
>can you please explain the logic of this program....

Notes:

[*1] Can you see why the loop steps by two?  If not, then count how
many stars there are in each row of the diamond.

[*2] Note the difference between print() and println().

If you really cannot understand the program, then work through it on
paper, keeping track of all the variables by hand.  That will give you
more insight into how it does its work.

rossum

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: PRINTING DIAMOND SHAPE WITH LOOPS! jyothi.m1089@gmail.com - 2013-11-01 01:30 -0700
  Re: PRINTING DIAMOND SHAPE WITH LOOPS! rossum <rossum48@coldmail.com> - 2013-11-01 13:03 +0000
  Re: PRINTING DIAMOND SHAPE WITH LOOPS! Mike Amling <mamling@chaff.us> - 2013-11-01 09:59 -0500
  Re: PRINTING DIAMOND SHAPE WITH LOOPS! Roedy Green <see_website@mindprod.com.invalid> - 2013-11-01 18:32 -0700
    Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-12 05:21 -0800
      Re: PRINTING DIAMOND SHAPE WITH LOOPS! Lew <lewbloch@gmail.com> - 2013-11-27 15:57 -0800
        Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-27 19:21 -0500
        Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-28 17:45 -0800
          Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-28 21:10 -0500
            Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-29 03:03 -0800
              Re: PRINTING DIAMOND SHAPE WITH LOOPS! Joerg Meier <joergmmeier@arcor.de> - 2013-11-29 12:33 +0100
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-29 09:01 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-29 13:15 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-29 17:06 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-29 17:24 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-29 20:59 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-30 06:57 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-30 10:31 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Lew <lewbloch@gmail.com> - 2013-12-02 15:32 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-12-04 03:45 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-12-09 21:10 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-12-05 00:15 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-12-09 21:14 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Joerg Meier <joergmmeier@arcor.de> - 2013-11-29 19:58 +0100
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-29 17:12 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Joerg Meier <joergmmeier@arcor.de> - 2013-11-30 02:55 +0100
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-29 21:01 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Lew <lewbloch@gmail.com> - 2013-12-02 15:31 -0800
              Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-29 12:48 -0500

csiph-web