Question

Powers of numbers can be calculated by multiplying the number by itself for as many times...

Powers of numbers can be calculated by multiplying the number by itself for as many times as the value of the exponent. For example, 2 raised to the power 4 can be calculated by multiplying 2 by itself 4 times to get 16. Write a program that:

1. inputs a double as the base number and an int as the exponent;
2. multiplies the base number by itself using a loop that repeats for the

number of times in the int;
3. outputs the exponential value calculated.

Use an if statement for the special case where the output is 1 if the int value is 0. For a more challenging version, deal with the case where the exponent is negative.

Programm language c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

int exponentPower(int a, int b){

int result = 1;

if(b==0)

return 1;

for(int i=1;i<=b; ++i)

result = result*a;

return result;

}


int main() {

cout<< exponentPower(2,3)<<endl;

cout<< exponentPower(2,4)<<endl;

}

==================================================

SEE OUTPUT

Files : main.cpp 6 D saving... 1 #include <iostream> 2 using namespace std; 3 int exponentPower(int a, int b) { clang version

Thanks, PLEASE COMMENT if there is any concern.

========================================================================

Add a comment
Know the answer?
Add Answer to:
Powers of numbers can be calculated by multiplying the number by itself for as many times...
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
  • 1) How many times is the fib method invoked for fb(5)7 a. 14 b. 15 c....

    1) How many times is the fib method invoked for fb(5)7 a. 14 b. 15 c. 25 d. 31 e. 32 2) Fill in the code to complete the following method for computing Fibonacei aumber publie statie long fib(long index) t if (index--0) Base case return 0; return 1 return else if (index1)/ Base case else I/ Reduction and recursive calls a. fib(index 1) b. fib(index-2) c. fib(index-1)+ fib(index -2) d. fib(index - 2) + fib(index -1) 3) What is...

  • ld ts biovs Part II: Analysis of recursive algorithms is somewhat different from that of non-recursive...

    ld ts biovs Part II: Analysis of recursive algorithms is somewhat different from that of non-recursive algorithms. We are very much interested in how many times the method gets called. The text refers to this as the number of activations. In inefficient algorithms, the number of calls to a method grows rapidly, in fact much worse than algorithms such as bubble sort. Consider the following: public static void foo ( int n ) { if n <=1 ow ura wor...

  • (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an...

    (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an integer between 1 and n. (2) Suppose Algorithm-1 does f(n) = n**2 + 4n steps in the worst case, and Algorithm-2 does g(n) = 29n + 3 steps in the worst case, for inputs of size n. For what input sizes is Algorithm-1 faster than Algorithm-2 (in the worst case)? (3) Prove or disprove: SUM{i**2} [where i changes from i=1 to i=n] ϵ tetha(n**2)....

  • 1. How many different ways can you have r numbers 1 sum up to a number...

    1. How many different ways can you have r numbers 1 sum up to a number n? These are called compositions of a number n and it is easy to calculate from our understanding of binomial coefficients. So the number of compositions of 4 into 3 parts will be 1+1+2, 1+2+1, and 2+1+1. Note how we think of 1+1+2 and 1+2+1 as different-because in the first case, the first number is 1, second is 1 and third is 2, while...

  • CAN SOMEONE HELP WITH THIS? Part 1: power() method We want to write a method that...

    CAN SOMEONE HELP WITH THIS? Part 1: power() method We want to write a method that will enable us to calculate a number to a given power. In other words, we are writing a method to mimic what the Math.pow() method we have seen before does. To write this method use the following requirements: The method should return an integer. It should have two parameters x and y both integers. The name of this method will be power(). For the...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • Please try to not use array lists Main topics: Random number generators Arrays Programmer defined methods...

    Please try to not use array lists Main topics: Random number generators Arrays Programmer defined methods Program Specification: You are to develop a program to play a variation on a game of chance called single player Poker. Game Description: • There is a Player who starts with 100 chips, each worth $1.00. • There is a deck of 36 cards: – Each card has a number in the range of [1, 9] printed on it - 9 possible values /...

  • python 11.8 Multiple committees The eventual goal of this assignment is to compute the total number...

    python 11.8 Multiple committees The eventual goal of this assignment is to compute the total number of ways of forming a collection of committees from an academic department of n professors. The first task is to write a function called committee to compute the number of ways of forming a single committee with members from a collection of n people. This is the same as compușing the number of ways of choosing r people out of n to sit at...

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

  • Please read the case "Is JCPenney Killing Itself with a Failed Strategy?"1 in your textbook and...

    Please read the case "Is JCPenney Killing Itself with a Failed Strategy?"1 in your textbook and answer the following questions. Type your answers on a Word Document (make sure to include your Name Surname and Student ID Number on it) and submit to the Homework inbox on Moodle. Questions: Each question is worth 25 points. 1. What (business level) strategy was the new CEO at JCPenney trying to implement given the generic (business level) strategies found in Chapter 4. 2....

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