Computer Science, asked by PreBoard1289, 1 year ago

What is recursion? Write a C program for factorial of n using recursion.

Answers

Answered by jeevan9447
5

Factorial is the product of an integer and all the integers below it; e.g. factorial four ( 4! ) is equal to 24.


#include <stdio.h>

long int factorial(int n);

int main()

{

   int n;

   printf("Enter a positive integer: ");

   scanf("%d", &n);

   printf("Factorial of %d = %ld", n, factorial(n));

   return 0;

}

long int factorial(int n)

{

   if (n >= 1)

       return n*factorial(n-1);

   else

       return 1;

}

Answered by VemugantiRahul
3
Hi there!
Here's the answer:

¶ Recursion:
'Recursion' is a repetitive process in which function calls itself.
Eg: Factorial, Fibonacci series and Towers of Hanoi etc.

¶ Recursion is of Two types:
•Direct Recursion
Here, Function calls itself.
• Indirect Recursion
Here, Calling Function calls Called Function and Called Function calls Calling Function.

•In general, Programmers has to use approaches to write repetitive algorithms. One such approach uses 'Loops', the other uses 'Recursion'.

•°•°•°•°•°•°<><><><> •°•°•°•°•°•°

Now ,
Consider Example of Recursion
-----¶¶ Factorial ¶¶-----

¶ Factorial (Iterative) Definition :
(Iterative - Using Loops)
The factorial of a no. is the product of Integral valises from 1 to the given No.

•factorial(n)
= 1; if n= 0
[Base case]
= n*(n-1)*(n-2)*…3*2*1; if n>0
[General case]

•°•°•°•°•°•°<><><><> •°•°•°•°•°•°

¶ Factorial Recursion definition

•factorial(n)
= 1; if n= 0
[Base case]
= n* factorial(n-1); if n>0
[General case]


•°•°•°•°•°•°<><><><> •°•°•°•°•°•°

/*C- Program Example for factorial using Recursion*/

#include<studio.h>
int fact(int n)
{
…int f
…if(x==0||n==1)
……return(1);
…else
……return(x*fact(x-1));
}
void main()
{
…int n,res;
…printf("\n Enter a No.:");
…scanf("%d",&n);
…res= fact(n)
…printf("%d",res);
…getch()
}

----------<><><><><><>----------
:)
Hope it helps
Similar questions