How to calculate factorial using recursion in C#

2 Answers

0 votes
using System;

public class Program
{
    public static int factorial(int n) {
        return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
    }

    public static void Main() {
        int n = 5;

        Console.Write(factorial(n));
    }
}



/*
run:

120

*/

 



answered Aug 5, 2022 by avibootz
0 votes
using System;
 
public class Program
{
    static int factorial(int n) => n == 0 ? 1 : n * factorial(n - 1); 

    public static void Main() {
        int n = 5;
 
        Console.Write(factorial(n));
    }
}
 
 
 
/*
run:
 
120
 
*/

 



answered Mar 4, 2023 by avibootz

Related questions

2 answers 240 views
1 answer 126 views
2 answers 171 views
1 answer 146 views
2 answers 210 views
1 answer 153 views
1 answer 146 views
...