Question

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.

  1. Given one number, calculate the factorial of that given number. i.e. 7! = 5,040
  2. Given one number, and one power, calculate the number raised to that power: i.e. 5 ^ 2 =25.
  3. Given two numbers, calculate the GCD. i.e. gcd (14, 21) = 7.

The program will have to calculate these using TWO methods, iterative, and recursive, so display the result as calculated using each method.

  1. 9!
  2. 14!
  3. 6 ^ 12
  4. 2 ^ 24
  5. gcd (325, 481)
  6. gcd (203, 2523)

you may find the Binary GCD and Euclidean algorithms to be useful in guiding your code for the GCD problem.

sample output:

Iterative Factorial 9 : 362880 Recursive Factorial 9 : 362880 Iterative Factorial 14 : 87178291200 Recursive Factorial 14 : 8

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

CODE:

#include<iostream>
using namespace std;
int fact(int num){
   if(num==0)
       return 1;
   return num*fact(num-1);
}
int power(int num,int pow){
   if(pow==0)
       return 1;
   return num*power(num,pow-1);
}
int gcd(int num1,int num2){
   static int res=num2;
   if((num1%res==0)&&(num2%res==0))
       return res;
   res--;
   gcd(num1,num2);
}
int main(){
   int i,num1=12,num2=6,fact1=1,res;
   for(i=num1;i>0;i--)
       fact1*=i;
   cout<<"Iterative Factorial "<<num1<<" : "<<fact1<<endl;
   cout<<"Recursive Factorial "<<num1<<" : "<<fact1<<endl;
   res=num1;
   while((num1%res!=0)||(num2%res!=0))
       res--;
   cout<<"Iterative GCD ("<<num1<<","<<num2<<") : "<<res<<endl;
   cout<<"Recursive GCD ("<<num1<<","<<num2<<") : "<<gcd(num1,num2)<<endl;
   res=1;
   for(i=1;i<=num2;i++)
       res*=num1;
   cout<<"Iterative Power "<<num1<<"^"<<num2<<" : "<<res<<endl;
   cout<<"Recursive Power "<<num1<<"^"<<num2<<" : "<<power(num1,num2)<<endl;
   return 0;
}

OUTPUT:

C:\Program Files (x86)\Dev-Cpp ConsolePauser.exe Iterative Factorial 12 : 479001600 Recursive Factorial 12 : 479001600 Iterat

Add a comment
Know the answer?
Add Answer to:
In C++, you’ll be generating a program that performs several different functions in two different manners,...
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++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g...

    Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g edi ) and gcdr , which both take two integers a, b and calculates their greatest common divisor (GCD) using the Euclidean algorithm gcdi () should do so using iteration while gcdr () should use recursion. Then write a third function, gcd(), which takes two integers a, band an optional third argument nethod which takes a charater string containing either "iterative" or "recursive", with...

  • Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...

    Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2,… . Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop). The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs...

  • PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which...

    PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which will simulate a calculator. Your calculator needs to support the five basic operations (addition, subtraction, multiplication, division and modulus) plus primality testing (natural number is prime if it has no non-trivial divisors). : Rewrite your lab 3 calculator program using functions. Each mathematical operation in the menu should be represented by a separate function. In addition to all operations your calculator had to support...

  • Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from...

    Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from a file that contain positive integers and should insert those numbers into the RB tree in that order. Note that the input file will only contain distinct integers. Print your tree by level using positive values for Black color and negative values for Red color Do not print out null nodes. Format for a node: <Node_value>, <Parent_value>). For example, the following tree is represented...

  • You may import the following library functions in your module: from fractions import gcd from math...

    You may import the following library functions in your module: from fractions import gcd from math import floor from random import randint You may also use: • the built-in pow() function to compute modular exponents efficiently (i.e., ak mod n can be written in Python as pow(a,k,n)), • the sum() function returns the sum of a list of integers (sum(1,2,3,4) returns 10). problem 1 a. Implement a function invPrime(a, p) that takes two integers a and p > 1 where...

  • Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine...

    Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine library procedures Objectives: 1. using register indirect addressing 2. passing parameters 3. generating “random” numbers 4. working with arrays Description: Write and test a MASM program to perform the following tasks: 1. Introduce the program. 2. Generate ARRAYSIZE random integers in the range [LO = 10 .. HI = 29], storing them in consecutive elements of an array. ARRAYSIZE should be set to 200....

  • The answer need to write in C programming. QUESTION 2 Write a program using array to...

    The answer need to write in C programming. QUESTION 2 Write a program using array to generate a multiplication table based on the user's input. For example if user enter 5 as input then a multiply table for 1 to 5 is printed as output as shown in Figure Q2. There are three main steps in this program which are: Print rows (a) (b) Print columns Print multiplication of data inside table (c) Select C:\example1 \bin\Debuglexample 1.exe enter the value...

  • Write a C or C++ program A6p2.c[pp] that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 39 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12]

    1.      Write a C or C++ program A6p2.c[pp] that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 39 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to convert all 60 array elements modulo 11 (i.e. take the remainder after division by 11) in place. You should divide this update task among the n threads as evenly as possible. Print the array both before and after...

  • Write a Python program that tests the function main and the functions discussed in parts a...

    Write a Python program that tests the function main and the functions discussed in parts a through g. Create the following lists: inStock - 2D list (row size:10, column size:4) alpha - 1D list with 20 elements. beta - 1D list with 20 elements. gamma = [11, 13, 15, 17] delta = [3, 5, 2, 6, 10, 9, 7, 11, 1, 8] a. Write the definition of the function setZero that initializes any one-dimensional list to 0 (alpha and beta)....

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