Forum > Programming

Recursion Program.

(1/1)

Kakashi Natsu:
So a friend asked me to help him with a program using Java. SO being a good friend, i did. Unknowingly i got myself into a heap of trouble... getting into recursion. Now for some of you, you may not know what recursion is in programming. Well here is an example of a factorial.
[/size]
[/size]
--- Code: ---




//will return an integer
number = 0;


Integer factorial(integer n){
if(n==0){
return number
}
number = n*factorial(n-1)




}
//lets call up this routine
factorial(3)
//this is 6 btw.



--- End code ---

[/size]
[/size]So This is a simple version of a recursive program, where in the program itself, it calls itself up. Confusing but very helpful when it comes to path finding. For those that have a JDK and can run Java. To see the program i helped my friend with, click on the zip folder->https://jetfire3201.000webhostapp.com/ and run it in netbeans or eclipse. Main program is in Easterbunny.java.

ThatsHawt:
in your factorial method, you should return an int instead of an Integer.
Every time you run this method it creates an Integer object which is a waste of space when you can just return an int.

Navigation

[0] Message Index

Go to full version