Create a program in C with a function \"factorial\" using the code given to you
ID: 3907666 • Letter: C
Question
Create a program in C with a function "factorial" using the code given to you in the book for recursively creating a factorial sequence 6-25.
You need to write main program which will prompt the user for "n" and then call the factorial function by value. Your returned value should be printed out to the user.
me Calculate the factorial of There is no test that the result Pre n is the number to Post result is returned lal of a number using a loop- the result fits in a long. number to be raised factorially long factorial (int n) // Local Declarations long facts = 1; 10 Statements for (int i = 1; facts = facts * i; iExplanation / Answer
Hello Student!
I am happy to help you!
Here goes the code :
#include <stdio.h>
// Recursive function for calculating factorial
long factorial(long int n) {
if (n == 1 || n == 0) {
return 1;
}else {
// Call recursively
return n*factorial(n-1);
}
}
int main(int argc, const char * argv[]) {
// Enter the value of n
printf("Enter the value of n :");
// Declaration of n
long int n;
// Taking input
scanf("%ld", &n);
// Printing factorial
printf("The factorial value is : %ld ", factorial(n));
return 0;
}
Test Case :
Enter the value of n :5
The factorial value is : 120
Program ended with exit code: 0
Thank you. Feel free to ask anything. Please upvote, if you like the answer. Thank you again.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.