6. Write a program to find factorial of a given number
Answers
Answered by
1
Answer:
हि
C program to find factorial of a number
long factorial(int);
int main() { int n;
printf("Enter a number to calculate its factorial\n"); scanf("%d", &n);
printf("%d! = %ld\n", n, factorial(n));
return 0; }
long factorial(int n) { int c; long r = 1;
for (c = 1; c <= n; c++) r = r * c;
return r; }
Answered by
0
Here's a Python program to find the factorial of a given number using a recursive function:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
# Test the function with an example input
number = 5
result = factorial(number)
print("The factorial of", number, "is", result)
Explanation:
- This program defines a function called factorial that takes an integer n as input and returns the factorial of n.
- The function uses recursion to calculate the factorial: if n is 0, it returns 1 (since 0! = 1), otherwise, it multiplies n by the factorial of n-1.
- In the main program, we test the function with an example input of 5 and print the result.
- You can replace the number with any other integer to find its factorial.
#SPJ3
Similar questions