Skip to main content

ProwessApps

Learn · Practice · Excel

WAP to find x to the power y

C++ Code Example — Basic Programs

ADVERTISEMENT

WAP to find x to the power y

Objective

Write a C++ program to calculate X to the power of Y (XY).

Algorithm / Approach

  1. Include the <math.h> header.
  2. Declare variables for the base x, exponent y, and the result.
  3. Read values for x and y from the user.
  4. Use the built-in power function: result = pow(x, y).
  5. Print the result.
main.cpp
#include<iostream>
#include<math.h>
using namespace std;
int main() {
 int x,y,result;
 cout<<"Enter Base: ";
 cin>>x;
 cout<<"Enter Power: ";
 cin>>y;
 result = pow(x,y);
 cout<<"RESULT = "<< result;
 return 0;
}

Expected Output

Enter Base: 5
Enter Power: 3
RESULT = 125

Explanation of the Program

  • The pow(base, exponent) function handles complex exponentiation mathematically.
  • Note: pow() technically accepts and returns floating-point numbers. Assigning its result directly to an integer variable (like in this code) will implicitly cast it and strip away any decimals, which is fine for whole-number exponents.

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT