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,...
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...
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...
Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to...
Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to n so that the first number is added, the second number is subtracted, the third number added, the fourth subtracted, and so on: 1-2+3-4+5-6+7… until you reach n. If n is 0 or less then return 0.
Write a function that returns the largest value in a stack (only use push and pop)
Write a function that returns the largest value in a stack (only use push and pop)
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. sample: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT