• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

oddSum

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to provide a code for oddSum that computes and returns, the sum of the odd numbers from 1 to n. n may be any positive integer that is passed as a parameter to the method.(eg. if the integer(n) that is passed in equals 10 the method should return the value 25 (1+3+5+7+9)

Please could someone help, I have an exam in 2weeks, i need to learn and understand this.I would appreciate it.
Thanks
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you have so far? Do you have an algorithm or even an outline of the steps to take?
A lot of people will be more than willing to help but they wont just provide an answer for a school assignment.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you go... try this....

I think it should work and it only took me a couple of minutes....
 
Rupinder Bains
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not a school assignment
This is a practise revision question for a uni exam i have got in 2weeks
and i have no idea how to answer it thats why i asked for ideas and help.
I am studying at Southampton University,UK and need to uderstand this
 
Rupinder Bains
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Eric, This is much appreciated.
Now Ill sit down and try to understand it. Thank you very much!!!
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key here is that your loop counters need not necessarily be incremented by one. For example, once you get the idea, you can get the sum of all multiples of, say, 4 starting from 0 to a given (but arbitrary) number N.
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recursive version
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if you tried oddSum(-1). I reckon this would recurse until a stack overflow occured. Another attempt at recursion:

I haven't had a chance to test this, so I'm sure it will be bug-ridden
[code now tested :roll: ]
[ August 21, 2002: Message edited by: Neil Laurance ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, why not

or


[ August 20, 2002: Message edited by: Jim Yingst ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic