Question

Using C++ write two Factorial functions. One using a loop and another using recursion. The program...

Using C++ write two Factorial functions. One using a loop and another using recursion. The program must be in module as well

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

#include <iostream>

using namespace std;

int fact_iter(int n)

{

int fact = 1;

for(int i=1;i<=n;i++)

fact = fact*i;

return fact;

}

int fact_rec(int n)

{

if(n==1)

return 1;

else

return n*fact_rec(n-1);

}

int main()

{

cout << "5! using iterative : " << fact_iter(5) << endl;

cout << "5! using recursive : " << fact_rec(5) << endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Using C++ write two Factorial functions. One using a loop and another using recursion. The program...
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
  • C++ Using Recursion We are going to create a (looping) menu that accesses functions that use...

    C++ Using Recursion We are going to create a (looping) menu that accesses functions that use recursion. The function headers and a description will be provided. You are responsible for defining the functions. Ensure that they each contain a base case they reach that doesn’t have a recursive function call, otherwise it’s an infinite loop! Functions must be implemented with recursion. int Factorial(int arg) Returns arg! (4! Is 4 * 3 * 2 = 24) Base case is Factorial(1) returning...

  • Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch...

    Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch statement and functions. 2. Write a program to find the summation of N numbers. Use two functions. One function will take the input from user and the other will perform the summation from 1 to N. 3. Write a program to find the factorial of a number. Use two functions. One function will take the input from user and the other will perform the...

  • Write a java program that demonstrates recursion. This program can be in a java GUI frame...

    Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.) The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line. Demonstrate recursion with a function called LoadTruck() that takes a number of boxes...

  • Recursion Write a program to solve the Towers of Hanoi problem for a tower of size...

    Recursion Write a program to solve the Towers of Hanoi problem for a tower of size n, using both recursion and iteration. Time each method separately. Be very carefull to time only the actual work and avoid superfluous module calls and initialization, etc. Compare and contrast your two versions of the problem. Are they what you expected? Your analysis must contain a table of the times obtained for each run. For a tower of a particular size, your output should...

  • Write a C++ program that reverses the digits of a given number using recursion

    Write a C++ program that reverses the digits of a given number using recursion

  • In C++, you’ll be generating a program that performs several different functions in two different manners,...

    In C++, you’ll be generating a program that performs several different functions in two different manners, iterative, and recursive. For the recursive portion of the program, you must use a function to pass data along. The program must be capable of doing the following, and without using the pre-existing mathematical functions for factorial, power, and so on. Given one number, calculate the factorial of that given number. i.e. 7! = 5,040 Given one number, and one power, calculate the number...

  • Given the incomplete program below, write two functions, one called integer and another called decimal. You...

    Given the incomplete program below, write two functions, one called integer and another called decimal. You can use following incomplete program. Your assignment is to supply the necessary missing code: #include #include using namespace std; int integer (….) { //REPLACE THE … WITH THE REQUIRED PARAMETER (A DOUBLE) //WRITE HERE THE CODE TO EXTRACT THE INTEGER AND RETURN IT } double decimal (….) { //REPLACE THE … WITH THE REQUIRED PARAMETER //WRITE HERE THE CODE TO EXTRACT THE DECIMAL PORTION,...

  • Write a program in C fro Binary Search tree using following functions 1. Insertion operation using...

    Write a program in C fro Binary Search tree using following functions 1. Insertion operation using recursion 2. Deletion operation 3. Minimum/Maximum of a BST 6. Reorganize the tree so that the tree height is minimum 7. Print all the nodes from the node to the path to the root 8. Find the lowest common shared node between two given nodes

  • In C++ using the while loop.. Compute factorial of a number Factorial of n = 1*2*3...

    In C++ using the while loop.. Compute factorial of a number Factorial of n = 1*2*3 *n 702

  • C++ program by using while loop structure. Write an interactive program (C++) to prompt and read...

    C++ program by using while loop structure. Write an interactive program (C++) to prompt and read two float type numbers, Then your program should perform and print the following arithmetic: (use while loop structure to repeat program 2 times. 1) Sum 2)Difference 3) Product 4) Quotient 5) Modulus (use system defined fmod function) You must use system's defined function prototype and/or to create programmer's defined function prototype for each of the above calculation. In addition, create a function called "DISPLAYLINE...

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