Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: can any one do this in a loop? (for loop)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default can any one do this in a loop? (for loop)

    >>>>>>>>>>>>>>>>>>>
    >>>>>>>>>>>>>>>>>>
    >>>>>>>>>>>>>>>>>
    >>>>>>>>>>>>>>>>
    >>>>>>>>>>>>>>>
    >>>>>>>>>>>>>>
    >>>>>>>>>>>>>
    >>>>>>>>>>>>
    >>>>>>>>>>>
    >>>>>>>>>>
    >>>>>>>>>
    >>>>>>>>
    >>>>>>>
    >>>>>>
    >>>>>
    >>>>
    >>>
    >>
    >

    or just like this

    >>>>>
    >>>>
    >>>
    >>
    >


    all of those you see are just a simple character '>' (greater than)

    can anyone do this in a for loop?..
    please im having some hard time thinking on how to diplay this...


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: can any one do this in a loop? (for loop)

    Easy, just have a for loop count backwards. However, to do it in one for loop is kind of hard (though not impossible)

    for (int i = 10; i > 0; i--)
    {
         for (int j = i; j > 0; j--)
         {
              System.out.print(">");
         }
         System.out.println("");
    }

    int n = 10;
    for(int i = (n*n+n)/2; i > 0; i--)
    {
         if (i*2%(n*n+n)==0)
         {
              n--;
              System.out.print("\n>");
         }
         System.out.print(">");
    }

  3. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    chronoz13 (October 3rd, 2009), Freaky Chris (October 3rd, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: can any one do this in a loop? (for loop)

    i can understand the second code a little bit . but i cant resolve this problem 'ArithmeticException'

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: can any one do this in a loop? (for loop)

    oops... flawed logic I was only checking if something was divisible by (n^2+n)/2, not equal to it.

    		int n = 10;
    		for (int i = (n * n + n) / 2; i > 0; i--)
    		{
    			if (i == (n * n + n) / 2)
    			{
    				n--;
    				System.out.print("\n>");
    			}
    			else
    			{
    				System.out.print(">");
    			}
    		}

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (October 4th, 2009)

  7. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: can any one do this in a loop? (for loop)

    you're really awesome when it comes in algorithms... lol tnx !!

    any way you said that this is possible in just one single for-loop...

    hmm....

Similar Threads

  1. JAVA for loop
    By tazjaime in forum Loops & Control Statements
    Replies: 2
    Last Post: August 18th, 2009, 08:43 PM
  2. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 10:10 AM
  3. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 13th, 2009, 12:47 AM
  4. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 05:31 AM
  5. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 03:31 PM