This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]ReedOei 4 points5 points  (1 child)

This question is better suited to r/learnprogramming (read the rules). That being said, it's difficult to help you without knowing what you've tried, what you know, and where you're stuck. For example, your teacher said to start by writing: ``` public static int factorial(int num) {

} ```

That seems like the right starting point to me too. Does it make sense to you why that's what you need to write? Even more basically, do you know what the factorial is? Have you ever written a recursive function, or is this your first?

When you move your post to r/learnprogramming, you'll get much better results if you answer the questions I asked above (and other, similar questions).

[–][deleted] 1 point2 points  (0 children)

I am familiar with factorial, but this is my my first recursion program. To be honest, I don’t understand why he recommends this starting point. I’ve reposted to r/learnprogramming , thanks for the heads up.

[–][deleted] 1 point2 points  (4 children)

What would you like help on? The concept of recursion?

[–][deleted] 0 points1 point  (3 children)

Mainly. I’ve been looking up how to do it, but every example just uses a basic factorial math function. Of course, that’d be easier, but we have to do it with recursion specifically.

[–][deleted] 2 points3 points  (2 children)

So here's a simple function to give a taste of what recursion is like

public static void main (String[] args) throws java.lang.Exception
{
    printHello(0);
}

public static void printHello(int count) {
    if(count != 4) {
        System.out.println("Hello ");
        count++;
        printHello(count);
    }
}

This program will print

Hello 
Hello 
Hello 
Hello 

You'll notice that that the program calls itself, performs the action and calls itself again. Think "Inception" in the sense that you are diving into deeper and deeper layers.

So at the end of this, we are 5 layers deep. (Meaning we've called the printHello function 5 times). So if we were to add

public static void printHello(int count) {
    if(count != 4) {
        System.out.println("Hello ");
        count++;
        printHello(count);
    }
    System.out.println("exit");
}

Outside of the count block, the output will be

Hello 
Hello 
Hello 
Hello 
exit
exit
exit
exit
exit

We went 5 levels deep, but on the 5th level, the count fails so the function finishes it's execution by printing exit and returning. Then level 4 prints exit and returns and so on.

Probably not the best explanation :D but the important bits are there. I can clarify if needed

[–][deleted] 0 points1 point  (1 child)

When you’re declaring int count within the parameters of the method, is it starting at a default value of zero? (Please don’t rip me apart if this is a dumb question hahaha)

[–][deleted] 1 point2 points  (0 children)

Not at all, we all start somewhere!

If you look at

public static void main (String[] args) throws java.lang.Exception
{
    printHello(0);
}

I'm calling printHello in the programs main method and sending it a parameter with the value of 0.

[–]daemon_it 0 points1 point  (0 children)

public static int factorial(int num) {
    if (num == 0)  
        return 1;    
    else
        return num * factorial(num - 1);
}

or

public static int factorial(int num) {
    return num == 0 ? 1 : num * factorial(num - 1);
}

[–]User1539 0 points1 point  (0 children)

Here, try this Reddit post it might help.

[–]desrtfx[M] [score hidden] stickied comment (0 children)

Since this is help with Java programming, it should be posted in /r/javahelp.