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?

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"?

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

commented: Do not post full code, and the answer should, at least, be relevant. -2

@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.

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

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?

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

commented: Once again: help him figure it out, don't dump it in his lap +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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.