What part are you having trouble with?
Most people here aren't interested in writing your code for you, nor do we want to guess at where your difficulty lies.
Do you know what factorial is?
Do you know a recursive definition of factorial, independent of Java?
Do you know how to write a method in Java?
Do you know how to do multiplication in Java?
etc.
i was thinking about somthing like this:
import java.util.*;
public class Recursion
{
int r;
public void main(){
System.out.println("Enter An Integer");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
factorial(n);
System.out.println(n);
}
public void factorial(int n){
if(n > 0){
r = n*(n-1);
factorial(n-1);
}
}
}
> i was thinking about somthing like this:
Did you compile and run it? (Lemme guess - no, it doesn't compile.)
What answer did it give you? (None, since it won't compile.)
Are you satisfied with the result? (Nope.)
If not, what do you plan to do about it?
Better think again. That code is wrong.
%
Learn
import java.util.Scanner;
class RecursiveTester
{
int factorial(int n)
{
if(n==1)
return 1;
return n * factorial(n-1);
}
}
class Recursive
{
public static void main (String[] args)
{
int num;
Scanner s = new Scanner(System.in);
System.out.println("Enter a number ");
num = s.nextInt();
RecursiveTester test = new RecursiveTester();
int ans = test.factorial(num);
System.out.println("The factorial for " + num + " Is " + ans);
}
}
Output
Enter a number
4
The factorial for 4 Is 24
i think the program should terminate.
if(n==1)
return 1;
else if(n<=0)
System.exit(0);
return n * factorial(n-1);
or
else if(n<=0)
return 0;
return n * factorial(n-1);
Message was edited by:
fastmike
Message was edited by:
fastmike
> i thought he could study from the code i posted not
> to submit the same thing. if he did submit my code
> then sooner or later he will get caught by getting a
> big F in his course.
I used to think that, but I noticed that the same people would come back again and again, asking for forum members to do other homework assignments. So they learned not to think. They learn that sooner or latter someone on the forum would do their homework for them. I'd like to hope that such people would get caught, but I honestly doubt it happens often enough to deter such people.
> Appologies Doc will never happen again. i am also a
> newbie just trying to increase my skills in java .
That's cool. There are plenty of questions here where you can do that. I'd just like to see help tempered with not giving away too much. When the end of semester looms at Unis (and finals for AP High School courses) this forum gets flooded with "do my homework" posts.
> Appologies Doc will never happen again. i am also a
> newbie just trying to increase my skills in java .
Then perhaps giving a discussion on the principles and general structure of recursive functions would have served better both you and the OP? Posting a couple of lines of trivial source code doesn't really make you or him/her any wiser.
no never and i will never claim myself to be very good in java. i can say this that i am better than most of the newbies over here. explanation always helps me out but in this program no need of explanation since i knew how the factorial function should work. the only difference b/w me and Op is that i was able to write a program for him so he can learn what mistakes he was making.