Question

3. Write a program to calculate factorials, n! Use a recursive function (i.e. a function that calls itself). If you use ints

use c++ and type it out as how you would use put it in code

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;
int factorial(int n) {
   if ((n==0)||(n==1))
      return 1;
   else
      return n*factorial(n-1);
}
int main() {
   int n;
   cout<<"Enter value of n: ";
   cin>>n;
   cout<<"Factorial of "<<n<<" is "<<factorial(n);
   return 0;
}

Result compiled and executed in 6.024 sec(s) Enter value of n: 16 Factorial of 16 is 2004189184

If we use int then the maximum value of n which will give correct result is 16,if we give n as 17 we will not get correct response.

If we use double then the maximum value of n is 170 for which we will get correct response,if we give n as 171 then we will get result as infinite.

Add a comment
Know the answer?
Add Answer to:
use c++ and type it out as how you would use put it in code 3....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Recursive Quadratic Design 2: draw a picture of the data and give the formula, and type...

    Recursive Quadratic Design 2: draw a picture of the data and give the formula, and type the algorithm for this program    Due: ___________________    ​RECURSIVE QUADRATIC - Program 2(100 points) CS1311 ​ Write a C program that finds the value of a specific quadratic function f(x) = ax^2 + bx + c where you pick a, b and c (before you program) to be non-zero integers. Use the function we worked with in class!  Do not put a, b or...

  • MUST BE ANSWERED BY USING C++ Please explain the code as well. Question 1 Write a...

    MUST BE ANSWERED BY USING C++ Please explain the code as well. Question 1 Write a recursive function defined by the following recursive formula: foo (Y, X) =     Y                                                            if X = 1     0                                                             if X = Y     (foo ( Y-1, X-1) + 3* foo ( Y-1, X))      if Y > X > 1 Write a driver to print out the value for foo (5, 4)    and foo (7, 5). In addition, print out the total number...

  • How do you write the code for a program to compare three variable int's in a...

    How do you write the code for a program to compare three variable int's in a void function with if/else statements only. The program takes 3 integers as input and outputs the integers sorted from least to greatest. Cannot use && or ||, only basic nested if/else statements. Please give details.

  • Q5 (25pts) Consider the code: int foo(int N){ if (N <= 3) return 2; int res1 = 3*foo(N-4); int...

    Q5 (25pts) Consider the code: int foo(int N){ if (N <= 3) return 2; int res1 = 3*foo(N-4); int res2 = foo(N-2); return res1-res2; } a) (6 points) Write the recurrence formula for the time complexity of this function (including the base cases) for N>=0. You do NOT need to solve it. b) (5 points) Draw the tree that shows the function calls performed in order to compute foo(8) (the root will be foo(8) and it will have a child...

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • Please help with this coding problem, and use the ARM sim# to test run your code....

    Please help with this coding problem, and use the ARM sim# to test run your code. Implement functions in assembly language. You are asked to write a program to compute the Fibonacci numbers, using recursive function calls. Fib(n){if (n == 0 || n == 1) return 1; else return Fib(n-2) + Fib(n-1);}

  • PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs...

    PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs to:             -define a recursive procedure/function and call it.             -use syscall operations to display integers and strings on the console window             -use syscall operations to read integers from the keyboard. Assignment Description: Implement a MIPS assembly language program that defines "main", and "function1" procedures. The function1 is recursive and should be defined as: function1(n) = (2*n)+9                      if n <= 5              =...

  • write programs with detailed instructions on how to execute. code is java What you need to...

    write programs with detailed instructions on how to execute. code is java What you need to turn in: You will need to include an electronic copy of your report (standalone) and source code (zipped) of your programs. All programming files (source code) must be put in a zipped folder named "labl-name," where "name" is your last name. Submit the zipped folder on the assignment page in Canvas; submit the report separately (not inside the zipped folder) as a Microsoft Word...

  • 1. Recursion is ususally where a function calls itself; (another example of recursion is where function...

    1. Recursion is ususally where a function calls itself; (another example of recursion is where function A calls function B, and B calls C and C calls A, creating a loop in the calls). Some problems are most naturally solved recursively, such as writing the factorial function, or finding all the perumutations of a sequence, or checking if a string is a palindrome. Since those examples were done in class, here we will give you a toy example, which normally...

  • # 3. PUT IN PYTHON CODE # Write a program that prints a count of how...

    # 3. PUT IN PYTHON CODE # Write a program that prints a count of how many occurrences of the letter "a" # there are in q. Do to this you will have to use a loop within a loop. q = ["apple", "beans", "orange", "tangerine", "butter", "watermelon", "vinegar"] # Type your answer here:

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT