Factorial of a number with & without using recursion
Factorial
of a number with & without using recursion
In
this article we will see how to calculate the factorial of a given number
Following
program is done without using recursion.
We can do this in multiple ways.
though I'm specifying a simple way to do it.
It's reader's responsibility to find out other ways.:)
We can do this in multiple ways.
though I'm specifying a simple way to do it.
It's reader's responsibility to find out other ways.:)
#include<stdio.h>
#include<conio.h>
void
main()
{
int n=0,fact=1,i;
clrscr();
printf("\n Enter the number
");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\n Factorial of the
Given number is = %d ",fact);
getch();
}
|
ΓΌ Following
program is done using recursion.(i.e. function calls itself in its own body)
#include<stdio.h>
#include<conio.h>
void
main()
{
int n,fact;
clrscr();
printf("\n Enter the number
");
scanf("%d",&n);
fact=factorial(n);
printf("\n Factorial of the
given number is %d ",fact);
getch();
}
int
factorial(int n)
{
int i;
if(n==0)
{
return 1;
}
else
{
return (n*factorial(n-1));
}
}
|
Output: We are
considering integer and character values and doing the type casting from one to
another, the output will be as follow,
Comments
Post a Comment