Question

In: Computer Science

Write a function called integerPower(base, exponent) that returns the value of baseexponent For example, integerPower(3,4) =...

Write a function called integerPower(base, exponent) that returns the value of baseexponent

For example,

integerPower(3,4) = 3*3*3*3

  • Assume that exponent is a non-zero integer, and base is an integer.
  • Function integerPower should use for loop to control the calculation.
  • Do NOT use any math library function.
  • Inputs for this program, which are base and exponent, have to be entered by the user

An example of input/output dialog is shown below:

Enter Integer Base: 5
Enter Integer Exponent: 3
5 raised to the power of 3 = 125

Enter 0 if you'd like to end this program.
Enter 1 if you'd like to run this program again: 1

Enter Integer Base: 2
Enter Integer Exponent: 4
2 raised to the power of 4 = 16

Enter 0 if you'd like to end this program.
Enter 1 if you'd like to run this program again: 0

Have a nice day!

Solutions

Expert Solution

#include<iostream>

using namespace std;

int integerPower(int base, int exponent)

{

    int ans = 1;

    int i;

   

    for( i = 1 ; i <= exponent ; i++ )

        ans *= base;

       

    return ans;

}

int main()

{

    while(1)

    {

        cout<<"Enter Integer Base: ";

        int base;

       

        cin>>base;

       

        cout<<"Enter Integer Exponent: ";

        int exponent;

       

        cin>>exponent;

       

        cout<<base<<" raised to the power of "<<exponent<<" = "<<integerPower(base, exponent)<<endl;

       

        cout<<"\nEnter 0 if you'd like to end this program.";

        cout<<"\nEnter 1 if you'd like to run this program again: ";

       

        int choice;

       

        cin>>choice;

       

        // if user wants to quit

        if( choice != 1 )

            break;

           

        cout<<endl;

    }

}


Sample Output


Related Solutions

Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
write an algorithm function called balance() in javascript that takes in a string and returns a...
write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns...
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns the highest similarity score found in the genome as a double. Note: the term genome refers to the string that represents the complete set of genes in an organism, and sequence to refer to some substring or sub-sequence in the genome. Your function MUST be named findBestSimScore Your function should take two parameters in this order: a string parameter for the genome (complete set...
In an exponential decay function, the base of the exponent is a value between 0 and 1. Thus, for some number b > 1, the exponential decay function can be written as
In an exponential decay function, the base of the exponent is a value between 0 and 1. Thus, for some number b > 1, the exponential decay function can be written as f(X) = a ∙(1/b)X. Use this formula, along with the fact that b = en, to show that an exponential decay function takes the form f(x) = a(e)−nx for some positive number n. 
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
Write a Python function that returns a list of keys in aDict with the value target....
Write a Python function that returns a list of keys in aDict with the value target. The list of keys you return should be sorted in increasing order. The keys and values in aDict are both integers. (If aDict does not contain the value target, you should return an empty list.) This function takes in a dictionary and an integer and returns a list. def keysWithValue(aDict, target): ''' aDict: a dictionary target: an integer ''' # Your code here
16. Write a function that returns the start value of a hailstone sequence that contains the...
16. Write a function that returns the start value of a hailstone sequence that contains the largest value that was reported by largestInAnyHS(n). Write a contract, then an implementation, of a function that takes exactly one parameter, an integer n, and returns the start value from 1 to n of the hailstone sequence that contains the largest value. The heading must be int startHSWithLargest(int n) This function must not read or write anything. Modify your main function so that it...
a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it...
a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it is palindrome or not. [2.5 marks] b. Write a c++ function, called GCD that receives two parameters n, d, and prints their greatest common divisor [2.5 marks] Now after you created the two functions, you have to write a main function that can call the above functions according to user choice. Write a menu that allows the user to select from the following options...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT