Question

Please implement it using recursion and it would be great if you could provide test code as well. Thanks.

1) Implement exponentiation recursively based on the following mathematical facts , if n = even number m = 2 1-1 1-1 n-1 2 m

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

int expo(const int m, const unsigned int n);

int main() {
    int base, power;
    printf("Enter base: ");
    scanf("%d", &base);
    printf("Enter power: ");
    scanf("%d", &power);
    printf("%d power %d is %d\n", base, power, expo(base, power));
    return 0;
}

int expo(const int m, const unsigned int n) {
    if (n == 0) {
        return 1;
    }
    int result = expo(m, n/2);
    if (n % 2 == 0) {
        return result * result;
    } else {
        return result * result * m;
    }
}
Add a comment
Know the answer?
Add Answer to:
Please implement it using recursion and it would be great if you could provide test code...
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
  • PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE...

    PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE TEST CODE FOR IT. 5) Design a recursive function to find the immediate successor of a target integer in an array o:f sorted integers. Here the immediate successor of a target is defined as the smallest number that is no smaller than the target in this sorted array int binarySuccessor(const int anArrayl, const int first, const int last, int target); In the above function...

  • Please help me with these, thank you! 3. **25.3 (Implement inorder traversal without using recursion) Implement...

    Please help me with these, thank you! 3. **25.3 (Implement inorder traversal without using recursion) Implement the inorder method in BST using a stack instead of recursion. Write a test program that prompts the user to enter 10 integers, stores them in a BST, and invokes the inorder method to display the elements. 4. **25.4 (Implement preorder traversal without using recursion) Implement the preorder method in BST using a stack instead of recursion. Write a test program that prompts the...

  • Recursion Intro Assignment. YOU HAVE TO DO IT USING "HELPER METHOD" AND NO IMPORTS ARE ALLOWED....

    Recursion Intro Assignment. YOU HAVE TO DO IT USING "HELPER METHOD" AND NO IMPORTS ARE ALLOWED. You may not use for or while. Any looping must be accomplished using recursion. You may not declare static or non-static member fields – only local or parameter variables allowed. Thanks! Implement the method public static int fibby (int n). fibby is mathematically defined for nonnegative values in the following way: fibby(0)1 fibby(n) - fibby(ln/4]) fibby(13n/4]) where n > 0 and x means the...

  • Please write down the code in JAVA. Objectives .To gain experience with using recursion by implementing...

    Please write down the code in JAVA. Objectives .To gain experience with using recursion by implementing the Ackermann's Function. To gain experience with using class variables to record method invocation history Description The Ackermann's function is of interest because it grows rapidly with respect to the size of m and n. It is the simplest example of a well-defined total function which is computable but not primitive recursive. This means it cannot be implemented using only for-loops. Notice that for-...

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

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

  • Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First...

    Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods: // PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are // reciprocal values of first n integers, with  alternating signs.      // For example,  sumTerms(7) returns  the following:  1/1 – 1/2  + 1/3 – 1/4  + 1/5 -1/6 + 1/7. // Terms with an odd denominator have positive sign, and terms with even denominator have // negative sign.  ...

  • Unrolling Recursion The objective of this problem is to simulate recursion using stacks and loops...

    Unrolling Recursion The objective of this problem is to simulate recursion using stacks and loops. A synthetic linear recursive procedure for this problem is provided in Code Fragment 1. A recursive function such as the one described is intuitive and easily understandable. On calling myRecursion(input), the execution first checks for the stopping condition. If the stopping condition is not met, then operations inside the recursive call are performed. This can include operations on local variables. The operations inside the function...

  • Recursion Exercises These exercises provide practice with recursion in Java. Please add notes if possible. Objectives...

    Recursion Exercises These exercises provide practice with recursion in Java. Please add notes if possible. Objectives Module: To write recursive solutions for basic problems that require iteration. To identify and address base cases in a recursive method. Reminders during development Each of your solutions to the problems below should be in their own method. If a method header is provided for a problem, then your solution should have that exact method header. Any changes to the method header will receive...

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

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