C Program to find the factorial of given number.
Factorial Of Given Number :
Factorial.c
#include<stdio.h>
int main(void)
{
int n,num;
long fact=1;
printf("Enter any number : ");
scanf("%d",&n);
num=n;
if(n<0)
printf("Invalid Number(-ve)\n");
else
{
while(n>1)
{
fact*=n;
n--;
}
printf("Factorial of %d is : %ld\n",num,fact);
}
return 0;
}
Output:
Terminal
Enter any number : 10
Factorial of 10 is : 3628800
Enter any number : -5
Invalid Number(-ve)
Factorial Of Given Number Recursive:
FactorialRecursive.c
#include<stdio.h>
long int factorial(int n);
int main(void)
{
int num;
printf("Enter any number : ");
scanf("%d", &num);
if(num<0)
printf("Invalid Number(-ve)\n");
else
printf("Factorial of %d is : %ld\n", num, factorial(num) );
return 0;
}
long int factorial(int n)
{
if(n==0)
return 1;
return(n*factorial(n-1));
}
Output:
Terminal
Enter any number : 5
Factorial of 5 is : 120
Enter any number : -5
Invalid Number(-ve)
Happy Learning 🙂