简体   繁体   中英

Factorial Recursion

I have searched the site, and though it has been answered many times, I still have one more question.

I have the code to do the factorial with recursion. I am just having trouble with the easiest part of it.

When printing, my project requires that it should print:

4! is equal to 4 x 3 x 2 x 1 = 24

How do I get a for loop, or recursive way to get the "(4 x 3 x 2 x 1)" to work with any value of n?

import java.util.Scanner;

public class Factorial 
{
    public static void main(String args[])
    {
        System.out.println("Enter an integer:");
        Scanner keyboard= new Scanner(System.in);
        int num=keyboard.nextInt();
        System.out.print(num+ "!"+ " is equal to ");
        Print(num);
        System.out.print(FactorialCalc(num));
    }

    public static double FactorialCalc(int number)
    {
        double result;
        if(number<=1)
        {    
            result= 1;                  
            return result;
        }    
        else
        {
            return result= number * FactorialCalc(number-1);
        }
    }

    public static void Print(int n)
    {
        for(int i=n; i<=0;i--)
        {
            System.out.print(n + 'x' + (n-1));
        }
    }
}
public static void Print(int n) {
    for (int i = n; i > 0; i--) {
        System.out.print(i);
        if (i == 1) {
            System.out.print("=");
            continue;
        }
        System.out.print("x");
    }
}

And the output:

Enter an integer:
4
4! is equal to 4x3x2x1=24.0

A very simple solution using a for loop will be

int fact=1;
for(int i=1;i<n;i++)
fact=fact*i;

Your code works, you only forgot one thing:

Which is the variable used for counting the iterations of the for loop in the Print method? What are its values inside the loop?

public static void Print(int n)
{
    for(int i=n; i<=0;i--) //i minor or equal 0? When does the loop need to finish?
                           //What happens if you multiply something with 0?
    {
        System.out.print(n + 'x' + (n-1));
    }

}

Try to get it on your own, but if you can't...

...the problem is that you're printing n instead of i . In the loop, the variable that is decremented is i via i-- . It starts from num and gets smaller and smaller... That's what you need to print!

Changhe the print to:

System.out.print(i + "x");

Your task is to get rid of the last printed x ! ;D

As per the loop condition, your loop must stop when i reaches 1 to have

(num) x (num-1) x .. x 2 x 1 (no 0!!)
So the condition will be for(int i = n; i >= 1;i--)

You could incorporate printing the list of multiplied values directly into the recursion rather than adding a looping print. Put appropriate print() statements in both the if and else clauses of your recursion. For the former, just print "1 = " . For the latter, print number + " x " .

You don't actually need the local variable result . I'd also recommend using Java conventions regarding capitalization: method names should begin with a lower case letter, upper case indicates a class or interface. Finally, I changed the return type to long because factorials are integer-based, even though they can quickly get big.

import java.util.Scanner;

public class Factorial {
   public static long printFactorial(int number) {
      if(number <= 1) {    
         System.out.print("1 = ");
         return 1;
      } else {
         System.out.print(number + " x ");
         return number * printFactorial(number-1);
      }
   }

   public static void main(String args[]) {
      System.out.print("Enter an integer: ");
      Scanner keyboard= new Scanner(System.in);
      int num=keyboard.nextInt();
      System.out.print(num + "! is equal to ");
      System.out.println(printFactorial(num));
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM