Part 5 Recursive function c# example

preview_player
Показать описание
Link for code samples used in the demo

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

What is a recursive function in c#? Give an example. This is a common c# interview question.
A recursive function is a function that calls itself. Let's look at an example of computing factorial of a number with and without recursion.

Program to compute factorial of a number without using recursion.
public class Program
{
public static void Main()
{
// Prompt the user to enter a number
Console.WriteLine("Please enter a number");
// Read the number from the console and convert to int
int userNumber = Convert.ToInt32(Console.ReadLine());
// Call Factorial function and pass the user entered number to it
double factorial = Factorial(userNumber);
// Print the factorial
Console.WriteLine("Factorial of " + userNumber.ToString() + " = " + factorial.ToString());
}

public static double Factorial(int number)
{
// ZERO factorial is 1, so return 1 if number is ZERO.
if (number == 0)
return 1;

double factorial = 1;

// Compute the factorial using a for loop
for (int i = number; i ]= 1; i--)
{
factorial = factorial * i;
}

// return the factorial of the anumber
return factorial;
}
}

Program to compute factorial of a number with recursion.
public class Program
{
public static void Main()
{
// Prompt the user to enter a number
Console.WriteLine("Please enter a number");
// Read the number from the console and convert to int
int userNumber = Convert.ToInt32(Console.ReadLine());
// Call Factorial function and pass the user entered number to it
double factorial = Factorial(userNumber);
// Print the factorial
Console.WriteLine("Factorial of " + userNumber.ToString() + " = " + factorial.ToString());
}

public static double Factorial(int number)
{
// ZERO factorial is 1, so return 1 if number is ZERO.
if (number == 0)
return 1;

// Notice that the Factorial method is calling itself here
return number * Factorial(number - 1);
}
}
Рекомендации по теме
Комментарии
Автор

Hi venkat i have been following your classes from long time your teaching skills and explanation is super and we could not get this much of subject by attending classes from any coaching institute very muck thanks for that .

balajisrinivas
Автор

Great tutorial, im new to C# and this recursion method got me confused however your video gave me a fair idea of recursion. Thanks man! :D

danielmafileo
Автор

thanx sir..ur gridview examples helped me a

saagarsoni
Автор

Thanks for this video. I have one question. What is the benefit using recursive function over normal function ? Which one is better approach ? Thanks.

prodiptamukherjeeBubai
Автор

Learner question here: At 3:33 - when writing to the console, what was the significance of changing the two ints with the .ToString() method? I've used them as ints just fine in a sequence of text like that so just wondering if I should be using this too for some reason.

ljs
Автор

It has been 4 years and nothing yet has been said about these subjects you've listed above...

LcsGomes
Автор

Hi Sir great video just wanted to ask one Question which was asked to me during interview like
What is the difference between recursive function and loops ?
Thanks in advance for the help :)

ishantiwari
Автор

Just a thought on 4.38 - I think the way factorial is calculated by recursion here is 4!= 4 x (4-1) x (3-1) x (2-1) = 24.

return number*factorial(number - 1)

yadurocky
Автор

Dear Venkat, you mentioned in your previous video, that you will explain, why are interfaces better in these aspects:
- loosely coupled systems
- dependency injection
- unit testing and mocking
Could you please paste here a link of these tutorials, if they are already uploaded somewhere?

Trzbne
Автор

thanks i miss course in c# developer your videos help me

mohamedabdlaal
Автор

and i have a question for you which is there in my mind for so much long "what is the difference between VARIABLE & FIELD & PROPERTY " please explain this theoretically and with examples of Real time, thanks in advance.(i think you gonna make a video on this question )

balajisrinivas
Автор

Nice! Just don't input a negative number. :-)

GeorgeMiscalencu
Автор

There is no need to call ToString on variables concatenated via the plus sign (+).

CuriousMindCenter
Автор

is it fair to say the crux of recursivce function is to imitate a mathematical formula iteration in
4factorial = 4 * 3 ( factorial(n-1 ) = 4-1) * 2 ( factorial(n-1 ) = 3-1) * 1 ( factorial(n-1 ) = 2-1) ...
so on until n==0 and it reaches break point,

but iam a bit confused as in when n==0 it only says return 1 ....doesnt than mean it will always return 1 because
eventualy n-1 will come to 1-1 =0 ... and that will go to the code if (n==0) then return 1

iam feel fucking stupid

BritishMoralHQ
Автор

Who the hell clicked the dislike button?

jayman