Logic:
- Enter the number n
- Product or Factorial= 1 x 2x 3 x 4 x………x n
Algorithm
- Enter the number as n
- Initially assign product=1
- for(i=1;i<=n;i++)
- product= product
Time Complexity: O(n)
Space Complexity: O(1)
Program
#include <stdio.h>
int fact(int n);
int main()
{
printf("****************************************************************\n");
printf("****************************************************************\n");
printf("** WAP to print factorial of a number n using function **\n");
printf("** Created by Sheetal Garg **\n");
printf("** Assistant Professor **\n");
printf("** Phone No:9467863365 **\n");
printf("****************************************************************\n");
printf("****************************************************************\n");
int n, f;
printf("\nEnter number to find factorial\n");
scanf("%d", &n);
f = fact(n);
printf("\nFactorial is %d", f);
}
int fact(int n)
{
int i, f = 1;
for (i = 1; i <= n; i++)
f= f * i;
return f;
}
Output 1
**************************************************************** **************************************************************** ** WAP to print factorial of a number n using function ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************************** **************************************************************** Enter number to find factorial 5 Factorial is 120