1,105,416 Community Members

Using recursion to get sum of numbers

Member Avatar
chiefpf
Newbie Poster
6 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I need to write several algorithms and one is giving me trouble. The one giving me issue requires the use of recursion to produce a summation for an integer. For example summation(4)=0+1+2+3+4=10.

The code I am working with is:

public long recursion(int N){
        if (N <=1)
        return 1;
        else    
        return N + ________ ;
    }

My question is the blank line above. How do I code the summation of N-1 to add to the value of N?

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

The definition of recursion (essentially) is to call a method from within that same method, so, knowing that, and knowing that you need to add N to N - 1 what do you think might be appropriate in that "blank"?

Member Avatar
prem2
Posting Whiz in Training
233 posts since Feb 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
-1
 

Dear chiefpf,

Try the below program .

import java.io.*;
import java.util.*;

class Recursion
{
        public static void main(String args[])throws IOException{
                System.out.println("\nRecursion Program");
                System.out.println("Enter the Number");
                BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
                int no = Integer.parseInt(obj.readLine());
                int result = 0;
                if(no == 0){
                        System.out.println("Nothing Happen");
                }else{
                        for(int i=1;i<=no;no--){
                                result = result + no;
                        }
                System.out.println("The Output is "+result);
                }
        }
}

Thank you,

Regards,
Prem

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

@Prem
I know you are trying to help, but if you give the OP a complete solution then what have you done? You have helped them cheat, and taught them nothing.
It's better to help by pointing him in a good direction, and suggesting relevant topics to read up.

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

Aside from the fact that the provided code has nothing to do with the posted question. ;-)

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

I should have looked more closely. :-(

@Prem
I know you are trying to help, but if you give the OP a completely incorrect solution then what have you done?

Member Avatar
prem2
Posting Whiz in Training
233 posts since Feb 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
-1
 

Dear JamesCherill && masijade,

I accept my mistake in my Post. I blindly wrote a small program and posted it .

(*)But for that you(masijade) will not put the negative points to my post.Mistakes may be happen at any time for any one.pls rememeber that.
(*)JamesCherill : He is a newbie poster .So i put the full code .To help that understand it.For advansed posters like you it is not necessary.


Here i have mention the sample example. It is not a full code.

public static  int recur( int number)
        {
                if ( number <= 1)
                     return 1;
                else
                     return(number+(recur(number-1)));

        }

Thank you,

With Regards,
Prem

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Hi Prem
Yes, that's much better code.
But please take note of the main point: if you give someone the answer to their homework the you are not really helping them. You should help them to gain the skills and knowledge that they need to do their own homework.

Member Avatar
prem2
Posting Whiz in Training
233 posts since Feb 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Dear JamesCherrill,

Yes i accept your point. He is a Newbie Poster.So only i have entered the full code.
Anyway here afer i try to put only Logics.Thanks for pointing my mistake.

Thank you,

With Regards,
Prem

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

I blindly wrote a small program and posted it .

(*)But for that you(masijade) will not put the negative points to my post.Mistakes may be happen at any time for any one.pls rememeber that.

Sorry, but with a join date from over a year ago you should, already, be fully familiar with our policies on that.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: