Results 1 to 8 of 8
  1. #1
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Thumbs down Solve my program

    Hi its me again Mika,

    I need a help again. Could you answer my problem,please? Our instructor give us another problem, he state: Create a recursive function that will calculate the sum of all odd integers from 1 up to a particular given number.
    Thank you very much...

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default couple of things

    • You have posted in the wrong part of the forum. This should probably be in the "New to Java" section. The mods can move it.
    • You probably already know this, but the forum won't do your assignment for you. The forum will help you to learn how to do it. To get you started, here's a way to detect if a number is odd/even:
    Java Code:
    if (x % 2 == 0) // number is even
    or
    Java Code:
    if (x % 2 != 0) // number is odd
    Do you know how/why the above code works ?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Default

    (i%2!=0) is the condition used to do this operation

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    (i%2!=0) is the condition used to do this operation
    it is quite strange for usually (i%1!=0) has been used. Try out the question

  5. #5
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    If you divide x by 1 It will always equal It self so there will be no remainder
    Example below

    Java Code:
    int x = 5;
    System.out.println(x % 1);
    Writes on the command line 0 because there is no remainder
    Java Code:
    int x = 5;
    System.out.println(x % 2);
    Writes on the command line .5 because .5 is the remainder of 5 / 2

    The Modulus(%) function finds the remainder of a division .
    but I would hope that you already knew that.

    Hope that helps,
    Sari
    If you don't try you can't succeed.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default ah... almost

    Actually that's not entirely true... it prints 1, not .5. The reason being because:
    • the remainder of 5/2 is not .5, it's 1


    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    I'm disappointed in myself messing up basic math oh well I think I got the point across
    If you don't try you can't succeed.

  8. #8
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Thumbs up mod % means reminder

    simple solution...
    multiplication is related with addition,
    division is related with subtraction,
    modulo is the remainder...

    for example,
    2*5 is 10
    how, 2+2+2+2+2 is 10...

    similarly division
    5/2 is 2 because you can subtract 2 only 2 times from 5
    (1st time)5-2=3 (3 remainder)
    (2nd time)3-2=1 (1 remainder)

    so, 5/2 is 2 and the remainder is modulo(%)...here its 1.
    understand the logic its very important...

Similar Threads

  1. Help me to solve problem
    By mansoorhacker in forum Forum Guides
    Replies: 8
    Last Post: 01-24-2009, 07:29 PM
  2. Solve my Problem
    By kyo in forum New To Java
    Replies: 1
    Last Post: 12-16-2008, 03:22 PM
  3. Help me to solve problem
    By mansoorhacker in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 09:15 AM
  4. Replies: 2
    Last Post: 08-27-2008, 03:40 PM
  5. Help mi solve my error
    By Deon in forum New To Java
    Replies: 3
    Last Post: 01-11-2008, 06:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •