Just by looking at the question, why would you want to do recursion for that? It would be much easier just to do in a method using Math.pow and adding the results.

There are 2 key ideas in recursive algorithms. The first is the problem must be something that can be broken down into simpler steps in an iterative manner. The second is the iteration must have a well defined stopping point. Factorial is a common example because it has a "natural" recursive solution. I.e., 5! = 5 * 4!, 4! = 4 * 3!, 3! = 3 * 2!, 2! = 2 * 1!, 1! = 1. So breaking the problem into simpler steps becomes n! = n * (n-1)!, and the stopping point is 1! = 1.

Understanding that recursive solutions are not particularly efficient (usually a loop performs better, at least in Java), both multiplication and addition problems can have recursive solutions. You just have to think about a simpler operation.

Multiplication can be accomplished by repeating addition, and Addition can be solved by repeatedly adding 1. The latter doesn't eliminate the addition operation, but it is simpler to just add 1, and this can be done using recursion.

One small suggestion:
This code will crash and burn horribly if presented with parameters like (3,2) or, more subtly, (-2,-3).
A simple replacement of the == on line 4 with a >= will protect against these nastys, as will the even simpler replacement of lines 4,5 with
if (n1>n2) return 0;

One small suggestion:
This code will crash and burn horribly if presented with parameters like (3,2) or, more subtly, (-2,-3).
A simple replacement of the == on line 4 with a >= will protect against these nastys, as will the even simpler replacement of lines 4,5 with
if (n1>n2) return 0;

Thanks, it did crash and burn.
So I changed my code

public static int sumSquares (int m, int n)
{
if (m==n)
return m*n;
if (m>n)
return 0;
return m* m + sumSquares(m+1, n);
}

If you have >= on line 6 you don't need lines 4,5 - just remove them and it will just recurse one more time and give the right result. I'm a great believer that the simplest code is usually the best.

If you have >= on line 6 you don't need lines 4,5 - just remove them and it will just recurse one more time and give the right result. I'm a great believer that the simplest code is usually the best.

What does it mean by recurse one more time? Because when i take out line 4,5 and do >= for line 6, it just gives me 0 even if the two numbers are equal, and for some of the numbers like m=1, n=2, it gives me wrong results like three.

Oh, very sorry, small mistake in my last post. It was right with just the > on line 6. This version is short & works:

public static int sumSquares (int m, int n) {
if (m>n) return 0;
return m* m + sumSquares(m+1, n);
}

My apologies.

Thanks! I see, so it works even if m and n are equal, it does the method again and does m*m+ sumSquares(m+1,n), which in this case will just give 0, so we get m*m.
The code is much simpler and the time complexity is more efficient!

Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...

OK, so HostGator for some reason no longer allows gcc/g++ access unless you have a Designated Server account, which is a lot of money to spend just to compile my "Hello World" program. Thus I figured I'd compile at home, then upload. Program is your regular old bare-bones Hello World ...

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.