answersLogoWhite

0

What is simulation recursion in C?

Updated: 8/11/2023
User Avatar

Wiki User

14y ago

Best Answer

The factorial f(n) = n * (n-1) * (n-2) * .. 1. For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the factorial of the parameter n. int factorial( int n) {

if (n==1)

return 1

else

return n* factorial( n-1) ;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

It is a function that calls itself.

Each time the function executes itself, it pushes a copy of itself onto the stack. This stack as a specific number of slots it can hold. A recursive function must either return before it has called itself x number of times, or it will cause a stack-overflow exception.

This is true in many C-like languages such as C++, Java, C#, Visual Basic .NET.

Example below:

# include

int factorial(unsigned int number)

{

// This is the exit clause. It guarantees a stack-overflow will not occur.

if(number <= 1)

{

return 1;

}

return number * factorial(number - 1);

}

void main()

{

int x = 5;

printf("factorial of %d is %d",x,factorial(x));

}

Each time a function calls itself, the overhead of it calling itself and adding itself to the stack increases. It is often more efficient to use a loop than use recursion. It is also less error-prone.

Example (many times faster without recursion):

int factorial(unsigned int number)

{

int factorialnumber = 1;

for(int index = number - 1; (index > 0) && (number > 0); --index)

{

// if number divides evenly into index... % operator is modulus, or division remainder.

if ((number % index) == 0)

{

factorialnumber = factorialnumber * index;

number = number / index; // this will never have a decimal.

}

}

return factorialnumber;

}

void main()

{

int x = 5;

printf("factorial of %d is %d",x,factorial(x));

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Well to explain it I'll use an example

power(int n,int exp)

{

if(exp==0)

{

return 1;

}

else

{

return n*power(n,exp-1);

}

}

so all you do is

function(parameters)

{

//check for base case

//if not base case then do the update and recursive call

}

This answer is:
User Avatar

User Avatar

Aanya Verma

Lvl 6
1y ago

Recursion is a powerful concept in computer programming that refers to a function calling itself. A recursive function is a function that calls itself, either directly or indirectly, to solve a problem.

A recursive function consists of a base case and a recursive case. The base case is the termination condition of the recursion. It is a condition where the function does not call itself and instead returns a value or performs some other action. In the recursive case, the function calls itself to solve a smaller instance of the problem.

Recursion can be used to solve problems that have a self-similar structure, such as searching a tree, finding the factorial of a number, or generating permutations of a string.

Let's take a simple example of a recursive function in C Programming to calculate the factorial of a number. The factorial of a non-negative integer n is the product of all positive integers from 1 to n.

Here is the code for a recursive function to calculate the factorial of a number:

int factorial(int n) {

// Base case

if (n == 0) {

  return 1;

}

// Recursive case

else {

  return n * factorial(n - 1);

}

}

In the code above, the factorial function takes an integer n as input and returns the factorial of n. The function first checks if n is equal to 0, which is the base case. If n is 0, the function returns 1. If n is not 0, the function enters the recursive case and calls itself with n-1 as input. The result of the recursive call is multiplied by n and returned as the result of the current call.

When the factorial function is called with a non-negative integer n, the function will keep calling itself recursively until n becomes 0. The base case is triggered at this point, and the function returns 1. Each recursive call's results are multiplied to calculate the final result.

It is important to note that recursive functions can be less efficient than non-recursive functions, as each recursive call requires additional memory for the function call stack. Additionally, recursive functions may lead to infinite recursion if the base case is not properly defined or if there is an error in the recursion logic.

In summary, recursion is a powerful concept in computer programming that can be used to solve problems that have a self-similar structure. Recursive functions in C programming consist of base and recursive cases. They can be used to solve problems such as searching a tree, finding a number's factorial, or generating a string's permutations. It is important to carefully define the base case and ensure that there is no infinite recursion to ensure that the recursive function works correctly.

Recursion can be very useful for solving problems that have a self-similar structure, but it can also lead to stack overflow errors and decreased efficiency. It can be used to write elegant and concise code, but it can also be difficult to debug and understand, especially for complex recursive functions.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Recursion, in any programming language, refers to, most commonly, a call to a function inside that function, (i.e a function calling itself)

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

w.a.p. to find factorial of a number using recursion

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

what is simulating recursion

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is simulation recursion in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between left recursion and right recursion in a grammar?

Recursion is what it's called when a function calls itself. When a function calls itself immediately before returning, it's called tail recursion. Tail recursion can be more efficiently written as iteration. In fact a good compiler will recognize tail recursion and compile it as iteration. There is no such thing as left or right recursion in C programming.


What is the C program for heap sort using recursion?

123


How many types of recursion are there in c language?

Recursion in c language is a method where the function calls itself, within or outside the scope. Using Recursion, complicated problems can be divided into smaller parts so that solving them becomes more manageable. The recursion technique is available in Java, JavaScript, and C++.serves the same purpose. The type of Recursion in C • Direct Recursion • Indirect Recursion. Direct Recursion Recursion can call the function n-number of times. In the case of direct Recursion, the function calls itself inside the same position or in the local scope Direct Recursion problems are the Fibonacci series, a program to print 50 natural numbers. Indirect Recursion In the case of Indirect Recursion, a function X calls function Y, and function Y calls any function Z. Under certain conditions, function Z calls function A. In this case, function A is indirectly related to function Z. Indirect Recursion is also known as mutual Recursion, as more than one function runs a program. It is a two-step recursive function call process for making a recursive function call. Below mentioned are also type of Recursion: Tail Recursion No Tail/Head Recursion Linear Recursion Tree Recursion Tail Recursion A function is said to be tail recursion if it calls itself and also calls the last or the previous statement executed in the process. Head Recursion A function is said to be Head Recursion if it calls itself and also calls the first or the beginning statement executed in the process. Linear Recursion A function is said to be a linear recursive function if it makes a single call to itself each time the procedure executes itself and grows linearly depending on the size of the problem. Tree Recursion Tree Recursion is different from linear Recursion. Rather than making only one call to itself, that function makes more than one recursive call to the process within the recursive function. Following are the steps to solve the recursive problem in C: Step 1: Create a function and assign the work a part should do. Step 2: Select the subproblem and assume that the function already works on the problem. Step 3: Get the answer to the subproblem and use it to resolve the main issue. Step 4: The 90% of the problem defined is solved.


What has the author C Pianese written?

C. Pianese has written: 'Numerical simulation of spatially developing forced and natural mixing layers with large eddy simulation' -- subject(s): Large eddy simulation, Mixing layers


What has the author Clifton C Lovell written?

Clifton C. Lovell has written: 'Simulation in field testing' -- subject(s): Air warfare, Simulation methods


What has the author C Moglestue written?

C. Moglestue has written: 'Monte Carlo simulation of semiconductor devices' -- subject(s): Computer simulation, Mathematical models, Monte Carlo method, Semiconductors


What is the use of recursion function?

Read the part in your programming manual/text book about recursion. The short answer while easy does not tell you anything about the power or dangers of recursion. It is the power and dangers of recursion that is important because once understood you can use recursion to good effect without running out of stack space.


What has the author Leonard C Guy written?

Leonard C. Guy has written: 'Simulation in library education' -- subject(s): Library education, Simulation games in education 'Decisions teaching package dissemination phase'


What is direct recursion?

When a function calls itself it is called as direct recursion. A function calls other functions which eventually call the original function is called as indirect recursion.


What has the author C T Wu written?

C. T Wu has written: 'Simulation and modeling of homogenious, compressed turbulence'


What type of science is recursion?

Recursion is a computer science. Typically computer programers write a specific program to test out a theory of recursion based on the known facts to try to define the variable.


What actors and actresses appeared in Recursion - 2013?

The cast of Recursion - 2013 includes: Andy Bolton as Ethan