• Top Posts

    C# program to calculate Factorial with recursive function


      class Program  
       {  
           static int Factorial(int number)  
           {  
             if (number < 1)  
             {  
               return 0;  
             }  
             else if (number == 1)  
             {  
               return 1;  
             }  
             else  
             {  
               return number * Factorial(number - 1);  
             }  
           }  
           static void Main(string[] args)  
           {  
           Console.Write("Enter a number: ");  
             if (int.TryParse(Console.ReadLine(), out int number))  
             {  
             Console.WriteLine(  
               $"{number:N0}! = {Factorial(number):N0}");  
             }  
             else  
             {  
               Console.WriteLine("You did not enter a valid number!");  
             }  
           Console.ReadLine();  
           }  
         }  
    

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728