Results 1 to 8 of 8
Thread: Sum of odd ints 0-n
- 12-06-2010, 06:03 AM #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?
Thanks in advance
- 12-06-2010, 06:18 AM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 12-06-2010, 06:22 AM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 12-06-2010, 06:27 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 12-06-2010, 06:52 AM #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?
- 12-06-2010, 07:40 AM #6
Member
- 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 = n-2; } else { n=n-1; } System.out.println(result);
- 12-06-2010, 08:24 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,415
- Blog Entries
- 7
- Rep Power
- 17
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:
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.Java Code:* <-- one pebble * * <-- three more pebbles * * * * * <-- five more pebbles * * * * * * * * * * <-- seven more pebbles * * * * * * * * * * * *
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-06-2010, 08:36 AM #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); } }
Similar Threads
-
Converting ints to Strings Problem
By gkoef in forum New To JavaReplies: 7Last Post: 12-06-2010, 01:07 PM -
Help with ints
By Insomniac Riot in forum New To JavaReplies: 5Last Post: 04-02-2010, 03:53 PM -
checking for ints in a String
By SteroidalPsycho in forum New To JavaReplies: 1Last Post: 03-26-2010, 06:09 PM -
GUI's and inputting doubles or ints
By lopder1 in forum New To JavaReplies: 19Last Post: 11-05-2009, 08:50 PM -
reading in unsigned ints into a 2D array
By newToIt in forum New To JavaReplies: 9Last Post: 03-06-2009, 12:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks