Question

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.

  1. int Factorial(int arg)

    1. Returns arg! (4! Is 4 * 3 * 2 = 24)

    2. Base case is Factorial(1) returning 1

  2. Int Factor(int arg)

    1. Displays the prime factors of arg, including repeats

    2. You are allowed to use your isPrime function from lab 4

    3. If factors apart from 1 and arg exist, they are <= arg/2

    4. If num%n == 0, then n is a factor of num

    5. Base case: Factor(n), where n is prime, which just displays the prime with cout

  3. string ObliterateString(string arg)

    1. Takes the argument and removes a random character from it

    2. New string is displayed then used for recursive call

    3. Base case: ObliterateString(“”) has no string to obliterate, no output necessary.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream >


using namespace std ;


int factorial( int arg ){

    if( arg == 1 )
         return 1;

    else
return (arg * factorial( arg- 1 ) ) ;


}

int isPrime( int arg ){

    for (int i = 2; i <= arg/2; ++i) {

        if( arg% i == 0 )
            return 0  ;
        else return  1 ;

    }
}


int Factor(   int arg ) {
cout<<" The prime factors of number   are :" ;
    for (int i = 2; i < arg   ; ++i) {
        // since 1 is neither prime nor composite
        // thats why starting loop from 2
        if( arg % i == 0 ) {
            if (isPrime(i))

                cout << i << "  ";

        }
    }

}

int main(){


cout<< " enter an positive integer number "<< endl ;
    int value ;
    cin>> value ;
   int fact =   factorial(value );
    cout<< " The factorial of entered number is "<< fact << endl ;

        Factor(value );
    return 0 ;


}

Add a comment
Know the answer?
Add Answer to:
C++ Using Recursion We are going to create a (looping) menu that accesses functions that use...
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
  • 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...

  • C++ Recursion Code a function which displays the first n prime numbers. The prototype is: void...

    C++ Recursion Code a function which displays the first n prime numbers. The prototype is: void prime (int) The number of primes to display is passed as the parameter. The function prime itself is not recursive. However, it should call a separate recursive helper function which determines if a given number is prime #include <iostream> using namespace std; void prime(int ) { } int main() {    prime (21);    return 0; }

  • C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the +...

    C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the + operator. //Pre : M and N are defined and N > 0. //Post: Returns M x N { int Prod; if (N == 1)     Prod = M;                       //base case else     Prod = M + Multiply(M, N - 1); //recursive step return Prod; } 2) Reverse #include <iostream.h> void Reverse(int N) //Displays string of length N in the reverse order //Pre : N...

  • C++ Language 1. Why would you prefer to use recursion? (Multiple answers or one answer) a...

    C++ Language 1. Why would you prefer to use recursion? (Multiple answers or one answer) a When the problem to be solved is naturally recursive, a recursive algorithm provides a clear and simple solution. b Since stack memory is faster than heap memory, and recursion uses stack memory, it is preferable to use recursion. c If a language, like LISP, or a subset of a language, like C++ templates, provides recursion but does not provide looping, then use recursion. d...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

  • 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....

  • Redefining Base Functions (C++) Create two simple C++ classes (Use separate files please and include appropriate...

    Redefining Base Functions (C++) Create two simple C++ classes (Use separate files please and include appropriate headers) Define a base class named "Venue" that holds the following member variables and functions: Type of venue (string), e.g. public venue, private venue, community venue Year opened (int) Capacity (int) Base price (float), holds the average ticket price for the venue Potential revenue (float), a function returning capacity * base price Next, define a class named "Theater" that is derived from the "Venue"...

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

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