# Solve my program

• 12-16-2008, 02:03 PM
Mika
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...
• 12-16-2008, 04:05 PM
CJSLMAN
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:
Code:

`if (x % 2 == 0) // number is even`
or
Code:

`if (x % 2 != 0) // number is odd`
Do you know how/why the above code works ?

Luck,
CJSL
• 01-19-2009, 06:51 AM
Thennarasu
(i%2!=0) is the condition used to do this operation
• 01-19-2009, 08:04 AM
Webuser
Quote:

(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
• 01-21-2009, 03:09 AM
sari
If you divide x by 1 It will always equal It self so there will be no remainder
Example below

Code:

```int x = 5; System.out.println(x % 1);```
Writes on the command line 0 because there is no remainder
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
• 01-21-2009, 03:21 AM
CJSLMAN
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
• 01-21-2009, 04:54 AM
sari
I'm disappointed in myself messing up basic math oh well I think I got the point across
• 01-21-2009, 07:20 AM
Thennarasu
mod % means reminder
simple solution...
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...