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

(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]
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.
Python 3 Calculate factorial Create a function that uses a loop to calculate the factorial of...
Python 3 Calculate factorial Create a function that uses a loop to calculate the factorial of a number. * function name: get_fact * parameters: num (int) * returns: int * operation: Must use a loop here. Essentially calculate and return the factorial of whatever number is provided. but: - If num is less than 1 or greater than 20, print "num is out of range" and exit the function - If num is not an int, print "invalid num parameter"...
Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing...
Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing zeros (ie: the number of zeros at the end of the factorial). Input: integer n , a non-negative integer where n ≤ 100 Output: integer x y , the concatenation of x and y, where x is the sum of the digits in n! and y is the number of the zeros in n!) Note, 0 ≤ x , y . Example: Consider the...
There is a Java program that is missing one recursive function: public class Factorial { /*...
There is a Java program that is missing one recursive function: public class Factorial { /* / 1 when n is 0; * fact(n) = | * \ n*fact(n-1) otherwise */ public static int fact(int n) { return 0; } /* Factorial Test Framework * * Notice the odd expected value for fact(20). It is negative because * fact(20) should be 2432902008176640000, but the maximum int is only * 2147483647. What does Java do when integers run out of range?...
In C++, write a program that uses array to calculate the factorial of a reasonable large...
In C++, write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT