Thread: Loop question

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Loop question


    Hi How would i do a loop where is the user inputs a number, say n, the number would need to be positive and then give the multiples of 2 before n.

    eg user inputs 10

    output=
    2
    4
    6
    8
    10

    ?? thanks
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    what have you tried?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by mathkid182
    Hi How would i do a loop where is the user inputs a number, say n, the number would need to be positive and then give the multiples of 2 before n.

    eg user inputs 10

    output=
    2
    4
    6
    8
    10

    ?? thanks
    Code:
    int n = 10; //set this to whatever number you want
    for (int i = 0; i <= n; i+=2)
    {
    System.out.println(i);
    }

IMN logo majestic logo threadwatch logo seochat tools logo