Results 1 to 8 of 8
Thread: Sum of odd ints 0n
 12062010, 07:03 AM #1Member
 Join Date
 Dec 2010
 Posts
 18
 Rep Power
 0
Sum of odd ints 0n
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?
Thanks in advance
 12062010, 07:18 AM #2Senior Member
 Join Date
 Mar 2009
 Posts
 552
 Rep Power
 6
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.
If the above doesn't make sense to you, ignore it, but remember it  might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
 12062010, 07:22 AM #3Senior Member
 Join Date
 Mar 2010
 Posts
 953
 Rep Power
 5
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
 12062010, 07:27 AM #4Senior Member
 Join Date
 Mar 2010
 Posts
 953
 Rep Power
 5
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
 12062010, 07:52 AM #5Member
 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?
 12062010, 08:40 AM #6Member
 Join Date
 Dec 2010
 Posts
 18
 Rep Power
 0
So, how about this code? it's doing the same thing..
Java Code:... int n = 0; int result = 0; ... while(n>=0) { if(n%2==1) { result = result + n; n = n2; } else { n=n1; } System.out.println(result);
 12062010, 09:24 AM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,728
 Blog Entries
 7
 Rep Power
 21
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 * * * * * * * * * * * *
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 12062010, 09:36 AM #8Member
 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=n2; } else { n=n1; } } System.out.println(result); } }
Similar Threads

Converting ints to Strings Problem
By gkoef in forum New To JavaReplies: 7Last Post: 12062010, 02:07 PM 
Help with ints
By Insomniac Riot in forum New To JavaReplies: 5Last Post: 04022010, 04:53 PM 
checking for ints in a String
By SteroidalPsycho in forum New To JavaReplies: 1Last Post: 03262010, 07:09 PM 
GUI's and inputting doubles or ints
By lopder1 in forum New To JavaReplies: 19Last Post: 11052009, 09:50 PM 
reading in unsigned ints into a 2D array
By newToIt in forum New To JavaReplies: 9Last Post: 03062009, 01:36 PM
Bookmarks