# Thread: Sum of odd ints 0-n

1. Member Join Date
Dec 2010
Posts
18
Rep Power
0

## Sum of odd ints 0-n

I'm trying to write a program for my class that computes the sum off all odd integers between 0 and n, a user defined arbitrary variable.

Everything should be ok except these blocks, which is where I'm guessing I'm having the issues...
... global variables
Java Code:
```int n = 0;
int result = 0;
int newnum = 0;
int count = n;

//... computational code

while(count>=0) {
if(n % 2 ==1)  {              //if n is odd
result = n + newnum;     //result updated
newnum = n;                 //newnum equal to new odd int n
n=n - 2;                        //n set to next odd number down
count = count - 1;          //count updated
}
Else if(n % 2 == 0) {     //if n is even
n = n - 1;                    //make n odd
count = count - 1;        //continue subtracting counter
}
}

system.out.println(result)
system.exit(0)```
//
So far, it does the first step of the program correctly as far as I can tell. If n is set to 5, it displays 8 (5+3). if n = 7, it displays 12 (7+5)
Why doesn't it continue?  Reply With Quote

2. Senior Member Join Date
Mar 2009
Posts
552
Rep Power
11

## What do you actually get when you run the program? Why the count variable? Wouldn't using n>=0 work better for the while loop? You seem to have a lot of unnecessary variables in your code.  Reply With Quote

3. Senior Member Join Date
Mar 2010
Posts
952
Rep Power
10

## Since count is the variable that will change each time through the loop, count is the variable you should check for evenness or oddness, and count is the variable you should add to result if it's odd. If it's even, you should do nothing and go on to the next iteration of the loop. I don't see any purpose for newnum.

-Gary-  Reply With Quote

4. Senior Member Join Date
Mar 2010
Posts
952
Rep Power
10

## By the way, if you're allowed to know anything about even and odd numbers when doing this exercise, you might profit by knowing that the odd integers start at 1 and go up by twos. Are you allowed to use a for loop?

-Gary-  Reply With Quote

5. Member Join Date
May 2010
Posts
90
Rep Power
0

## int count = n;
int result = 0;
if ((count%2) ==0)
count --;

while (count > 0)
{
result = result+count;
count = count -2;
}
system.out.println(result)

is it useful?  Reply With Quote

6. Member Join Date
Dec 2010
Posts
18
Rep Power
0

## Java Code:
```...
int n = 0;
int result = 0;
...
while(n>=0) {
if(n%2==1) {
result = result + n;
n = n-2;
}
else {
n=n-1;
}
System.out.println(result);```  Reply With Quote

7. ##  Originally Posted by falkon114 I'm trying to write a program for my class that computes the sum off all odd integers between 0 and n, a user defined arbitrary variable.
The solutions I've seen here are all so 'computerese'; the old Greek didn't have computers but they were smarter than we are nowadays; they could think and the only tools they had were pebbles in the sand. They did this:

Java Code:
```
*                <-- one pebble

*   *            <-- three more pebbles

*   *

*   *   *        <-- five more pebbles

*   *   *

*   *   *

*   *   *   *    <-- seven more pebbles

*   *   *   *

*   *   *   *

*   *   *   *```
If you add nine more pebbles you get another square shape. Suppose there are P(n) pebbles in a square; if you add n+1 more pebbles you have P(n+1) == P(n)+n+1 pebbles. The length of a side of a square with P(n) pebbles is (n+1)/2. So P(n)= (n+1)*(n+1)/4. So given n (odd), the sum of all the numbers 1+3+5+6+7+ ... == (n+1)*(n+1)/4. We don't need no steenkin' loops.

kind regards,

Jos  Reply With Quote

8. Member Join Date
Dec 2010
Posts
18
Rep Power
0

## Thanks for that pointer. I've been trying to think of an "equation" to represent it. I figured there had to be one, but.. Im not good with math =/
(yet)
rewrote my code, I realised my made it way too complex the first go around. here tis:
Java Code:
```while(n>0) {
if(n%2==1) {
result = result + n;
n=n-2;
}
else {
n=n-1;
}

}
System.out.println(result);

}
}```  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
•