//1st way
#include <stdio.h>
#include <conio.h>
int fact(int);
int main()
{
int num, f;
printf ("\n Enter a number: ");
scanf ("%d", &num);
f = fact(num);
printf (" \n Factorial of %d is: %d ", num, f);
}
int fact(int n)
{
if(n == 1)
{
return 1;
}
else
{
return (n*fact(n-1));
}
}
____________________________________________________________________
//2nd way
#include <stdio.h>
#include <conio.h>
int getFactorial(int n)
{
int local;
if (n > 1)
{
local = n * getFactorial(n-1);
return local;
}
else
return 1;
}
int main()
{
int num, f;
printf ( "\n Enter a number: " );
scanf ("%d", &num);
f = getFactorial(num);
printf ( "\n Factorial of %d is: %d ", num, f);
}
#include <stdio.h>
#include <conio.h>
int fact(int);
int main()
{
int num, f;
printf ("\n Enter a number: ");
scanf ("%d", &num);
f = fact(num);
printf (" \n Factorial of %d is: %d ", num, f);
}
int fact(int n)
{
if(n == 1)
{
return 1;
}
else
{
return (n*fact(n-1));
}
}
____________________________________________________________________
//2nd way
#include <stdio.h>
#include <conio.h>
int getFactorial(int n)
{
int local;
if (n > 1)
{
local = n * getFactorial(n-1);
return local;
}
else
return 1;
}
int main()
{
int num, f;
printf ( "\n Enter a number: " );
scanf ("%d", &num);
f = getFactorial(num);
printf ( "\n Factorial of %d is: %d ", num, f);
}
No comments:
Post a Comment