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 9 of 9

Thread: Three ways to print "-" character

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Three ways to print "-" character

    Find three ways to make the program below print 20 copies of the dash character '-' by changing /adding just one character.

    int i,n=20;
    for(i=0;i<n;i--){
    System.out.println("-");
    }

    I found two solutions but till now not able to find the third one. The two solutions are

    int i,n=20;
    for(i=0;i<n;n--){
    System.out.println("-");
    }

    and,

    int i,n=20;
    for(i=0;-i<n;i--){
    System.out.println("-");
    }

    Please help me out in finding the third solution.


  2. #2
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Three ways to print "-" character

    Use a "while" loop or even a "do while" loop will work good. Hence you found 2 more ways.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Three ways to print "-" character

    Quote Originally Posted by aprabhat View Post
    for(i=0;i<n;n--){
    for(i=0;-i<n;i--)
    Both for loops technically work but are a bit twisted and misleading.
    The standard/common way is:

    for (i=0; i<n; i++)
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  4. #4
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Three ways to print "-" character

     
    int i,n=20;
    for(i=n;i>0;i--){
    System.out.println("-");
    }

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Three ways to print "-" character

    hi andbin,I know the standard way to iterate through a for loop,this is a puzzle in which you are allowed to change only single character.So if i change my original loop

    for (i=0; i<n; i--)

    by

    for (i=0; i<n; i++)
    as suggested by you then it doesn't satisfy the one character condition.

    --- Update ---

    Hi ankurt,
    Thanks for the Reply but the solution provided by you doesn't satisfy the one character condition.you are not allowed to change more than one character.

  6. #6
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Three ways to print "-" character

    int i,n=20;
    for(i=0;i>-n;i--){
    System.out.println("-");
    }

    then maybe this works..

  7. #7
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Three ways to print "-" character

    Still you changed two characters, one by changing n to -n and second you change the less than "<" operator with ">".

    --- Update ---

    Still you changed two characters, one by changing n to -n and second you change the less than "<" operator with ">".

  8. #8
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Three ways to print "-" character

    Oh...I over looked that.

  9. #9
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Three ways to print "-" character

    is someone find any solution?

Similar Threads

  1. Understanding layers of system logic. Clarity for what "SDK" & "JDK" mean
    By MilkWetGhost in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2013, 01:25 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 11:30 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 02:22 AM
  5. [SOLVED] why does this code print 0.0 for the value and "F" as the letter grade?
    By etidd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 12th, 2010, 10:38 PM