Write a C# program to create a recursive function to find the factorial of a given number


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2.  
  3. class functionexcercise
  4. {
  5. static void Main()
  6. {
  7. decimal fact;
  8. Console.Write("Enter a number : ");
  9. int num = Convert.ToInt32(Console.ReadLine());
  10. fact = Factorial(num);
  11. Console.WriteLine("The factorial of number {0} is {1}", num, fact);
  12. Console.ReadLine();
  13. }
  14. static decimal Factorial(int n1)
  15. {
  16. // The bottom of the recursion
  17. if (n1 == 0)
  18. {
  19. return 1;
  20. }
  21. // Recursive call: the method calls itself
  22. else
  23. {
  24. return n1 * Factorial(n1 - 1);
  25.  
  26.  
  27. }
  28.  
  29. }
  30.  
  31. }
  32.  

Result

C# language techstudy

     





Related Topics




Popular Posts