Write a program to find the factorial of a given number using recursion and iteration .....
Program:
Using recursion...
#include<stdio.h>
#include<conio.h>
long int recur_factorial(int n);
void main()
{
int n;
long int fact;
clrscr();
printf("Enter the number to find the factorial of :");
scanf("%d",&n);
fact=recur_factorial(n);
printf("\nFactorial of %d is %ld",n,fact);
getch();
}
long int recur_factorial(int n)
{
return n>1?n*recur_factorial(n-1):1;
}
Using iteration....
#include<stdio.h>
#include<conio.h>
int fact_it(int i, int f);
void main()
{
int f=1,i,x;
clrscr();
printf("Enter the number to find the factorial of:");
scanf("%d",&i);
x=i;
for(i;i>0;i--)
f=fact_it(i,f);
printf("\nFactorial of %d is %d ",x,f);
getch();
}
int fact_it (int i,int f)
{
f=f*i;
return f;
}
Output:
0 comments:
Post a Comment