1. Member Join Date
Dec 2008
Location
Davao Oriental
Posts
29
Rep Power
0

## 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...  Reply With Quote

2. ## 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  Reply With Quote

3. ## (i%2!=0) is the condition used to do this operation  Reply With Quote

4. Senior Member Join Date
Dec 2008
Posts
526
Rep Power
0

## (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  Reply With Quote

5. ## 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  Reply With Quote

6. ## 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  Reply With Quote

7. ## I'm disappointed in myself messing up basic math oh well I think I got the point across  Reply With Quote

8. ## 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...  Reply With Quote

#### Posting Permissions

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