6 Aug 2019

  • August 06, 2019
  • Amitraj








* Recursion and Non - Recursion in C:- 

->Recursive function is a function which calls itself again and again. There’s really not any big difference between the two functions except for this ability of recursive functions to call itself usually to reduce loops.
Recursive functions are procedures or subroutines implemented in a programming language, whose implementation references itself and hence occupy constant stack.

->Non Recursive Function are procedures or subroutines implemented in a programming language, whose implementation does not references itself
Some other points are
  • A recursive function in general has an extremely high time complexity while a non-recursive one does not.
  • A recursive function generally has smaller code size whereas a non-recursive one is larger.
  • In some situations, only a recursive function can perform a specific task, but in other situations, both a recursive function and a non-recursive one can do it.





/* C Program to find factorial of a no. using recursion and non
recursion  */                     by Amit raj purohit
http://codevidyalay.blogspot.com

#include<stdio.h>
int factNR(int);
int factR(int);
int main()
{
int n,f=1;
printf("Enter a no:");
scanf("%d",&n);

f=factNR(n);
printf("Factorial using Non Recursion %d",f);

f=factR(n);
printf("\nFactorial using Recursion %d",f);
return 0;
}
int factNR(int n)
{
int f=1;
while(n>=1)
{
f=f*n;
n--;
}
return f;
}
int factR(int n)
{
if(n==0)
return 1;
else
return n*factR(n-1);
}

INPUT/OUTPUT:
Enter a no:5
Factorial using Non Recursion 120
Factorial using Recursion 120

Translate

Popular Posts