Loading, please wait...

A to Z Full Forms and Acronyms

What is Recursion in C# | C# Tutorials

Dec 09, 2020 C#, CSharp, 32731 Views
The recursive function or method is a very strong functionality in C#. A recursive method is a method which calls itself again and again on basis of few statements which needs to be true.

What is Recursion in C# | C# Tutorials

The recursive function or method is a very strong functionality in C#. A recursive method is a method which calls itself again and again on basis of few statements which need to be true.

Similarly, when a function calls itself again and again it is known as a recursive function.

The recursion property depends on two things:

  1. The satisfaction of the condition, which enables it to call itself again and again.
  2. The calling of function or method with parameters and receiving new parameter after every execution.

Understanding Recursion

 

The factorial of a number

When we try to find out the factorial of a number there are various ways to get the results. All these methods are forms of looping. For instance, if we want to find factorial of the number: 5. We can use for loop with conditions and get the result.

N! = N * (N-1)!

Example (without recursive method):

public long Factorial(int n)

{

    if (n == 0)

        return 1;

    long value = 1;

    for (int i = n; i > 0; i--)

    {

        value *= i;

    }

    return value;

}

But if we try to do the same thing, with the recursive method it gets shorter and clearer.

Example (with the recursive method):

public static double Factorial (int number){

}

First, we create a function that will have parameters to be passed inside. Inside the function,

if (number == 0)    

                return 1; 

we will have a condition that will keep track of when to end the recursion. Here it means wheb=n the number is 0, no need to proceed further and return 1. Then the last step would be

return number * Factorial(number-1);//Recursive call    

In the above code, it is showing that return the multiplication of the number and number -1 to where the function is called and along with it call the Factorial function again but this time pass number-1 as a parameter.

Code:

using System;   

   

namespace InterviewQuestionPart4   

{   

    class Program   

    {   

        static void Main(string[] args)   

        {   

            Console.WriteLine("Enter a Number");   

   

            //read number from user   

            int number =Convert.ToInt32(Console.ReadLine());   

   

            //invoke the static method   

            double factorial = Factorial(number);   

   

            //print the factorial result   

            Console.WriteLine("factorial of"+number+"="+factorial.ToString());   

   

        }   

        public static double Factorial(int number)   

        {   

            if (number == 0)   

                return 1;   

            return number * Factorial(number-1);//Recursive call   

   

        }   

    }   

}   

Output:

 

For code visit here

A to Z Full Forms and Acronyms