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 intLog of type Integer -> Integer that returns the exponent of the largest...
Write a function intLog of type Integer -> Integer that returns the exponent of the largest power of 2 less than its integer argument. This needs to be a haskell function
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. 
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
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 function called evenUp that returns the result of increasing the first even digit in...
Write a function called evenUp that returns the result of increasing the first even digit in a positive integer parameter by 1. (Your solution should use no more than 10 lines of code. Your function can return any convenient value of your choice if the parameter is not positive.) For example, a program that uses the function evenUp follows. public class P5 { public static void main(String args[]) { System.out.println(evenUp(1232)); // prints 1332 only the first even 2 changes System.out.println(evenUp(1332));...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT