Question

In: Computer Science

Write a recursive function to calculate and return factorial of a given number 'n'. in C...

Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining

Solutions

Expert Solution

ANSWER:

Code with explanation:

// Function to find factorial of a number n :
int find_factorial(int n)
{
   //Factorial of 0 is 1
   if(n==0)
      return(1);

   //Function calling itself: recursion
   //Here we call the function recursively for a number less than the number in the previous recursive call
   return(n*find_factorial(n-1));
}



//The code below is to call the above Factorial function and pass a value entered by the user, to check the output
#include<stdio.h>
int main()
{
    int num;
    //Ask user for the input and store it in num
    printf("\nEnter any integer number:");
    scanf("%d",&num);
    //Calling the factorial function here
    int fact = find_factorial(num);
    //Printing the factorial
    printf("\nfactorial of %d is: %d",num, fact);
}

Code: (Screenshot of executed code)

Code output:

Final clean function to copy:

// Function to find factorial of a number n :
int find_factorial(int n)
{
   //Factorial of 0 is 1
   if(n==0)
      return(1);

   //Function calling itself: recursion
   return(n*find_factorial(n-1));
}

So, this was the full Python code for the given question, with full explanation and execution and screenshots. Hope it helps. If it did, plz consider upvoting , it would mean a lot to me. Plz feel free to ask and clarify any doubts, THANKS and have a Great day :)


Related Solutions

Write a recursive function to implement the Factorial of n (n!). In the main, let the...
Write a recursive function to implement the Factorial of n (n!). In the main, let the user input a positive integer and call your function to return the result.
Write a C function to calculate and return the factorial value of any positive integer as...
Write a C function to calculate and return the factorial value of any positive integer as an argument. Then call this function from main() and print the results for the following input values: 2, 3,4, 5, 10, 15 What is the maximum value of an integer for which factorial can be calculated correctly on a machine using your algorithm?
Write a MIPS Assembly program function to calculate the factorial of an input number. Analyze the...
Write a MIPS Assembly program function to calculate the factorial of an input number. Analyze the program for the value 10 and compute the Execution time in a MIPS processor at 2.4GHz. The CPI is 1.
(1) Write a simple Lisp function factorial1 to computee factorial number of n recursivaly (where n...
(1) Write a simple Lisp function factorial1 to computee factorial number of n recursivaly (where n is an int >= 0). (2) Write a Lisp function factorial2 to computee factorial number of n (where n is an int >=0) recursivaly with a global variable (e.g., a list, an array, or a hash tble) to save the result to be used later (memorization) to compute next factorial number. Run the program with test case. For your test run, the following cases:...
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm? use java
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm?
Write a c program to calculate the factorial of a number using recursion    [8] Question five...
Write a c program to calculate the factorial of a number using recursion    [8] Question five Write a stack algorithm to POP an item                                                         [6] What does FRONT and REAR signify in a queue?                                                                 [6] Write an algorithm for a dequeue operation                                                                       [8]
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT