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.
(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.
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
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
Write a function such that given a number N, display the N*N multiplication matrix from 1...
Write a function such that given a number N, display the N*N multiplication matrix from 1 to N. Then, write a C++ program such that, it prints the multiplication matrices of numbers 1,4,7, and 10 using a loop concept. Check the sample output below, to see how the program should work. Make sure to have your output exactly the same as the below output.
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input...
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input and returns the product of numbers from 1 to n.
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT