Program to find Factorial of a Number
FACTORIAL:
The factorial of a number, is the product of all positive integers less than or equal to n. It's calculated by multiplying all the integers from 1 to n, in order.
PROGRAM:
#include <stdio.h>
int main()
{
int i,n, fact=1;
printf("Enter a number: ");
scanf("%d",&n);
for (i=1;i<=n;i++)
fact=fact*i;
printf("Factorial of %d is %d.",n,fact);
return 0;
}
OUTPUT:
TIME COMPLEXITY:
The time complexity is O(n) because the loop iterates from 1 to n, performing n iterations. This means the time taken to execute the program increases linearly with the input size n.
SPACE COMPLEXITY:
The space complexity is O(1) because the program uses a fixed amount of memory to store the variables fact and i, regardless of the input size n. The memory usage doesn't grow with the input size, so it's constant.
Comments
Post a Comment