











C++ program to the Power of a number
Program to find the Power of a number
To calculate the power of a number we need the two inputs – base and exponent. These two inputs are passed into a function, pow(input1,input2), as parameters and it will return the result . The function is present in the math.h header file.
For Example: Base – 5 , Exponent – 3
pow(Base, Exponent) = pow(5, 3)= 125




Algorithm:-
- Take two inputs from user
- Inputs are stored in two double type variable say base & exp.
- A function, pow(base, exp) is called with base and exp as parameters.
- The calculated power is stored in another variable say res.
- res is printed with precision set up to 2 decimal places.
C++ Code:-
//C++ Program
//Power of a number
#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;
//main program
int main()
{
double exp, base;
cout<<“Enter base: “;
//user input 1
cin>>base;
cout<<“Enter Exponent: “;
//user input 2
cin>>exp;
//calculating power using function
double res = pow(base, exp);
//printing result
cout << base << “^” << exp << ” = “ ;
cout << fixed <<setprecision(2)<<res<<endl;
return 0;
}
Output
Enter base: 123
Enter Exponent: 3.5
123^3.5 = 20638013.40
- Positive or Negative number: C | C++ | Java
- Even or Odd number: C | C++ | Java
- Sum of First N Natural numbers: C | C++ | Java
- Sum of N natural numbers: C | C++ | Java
- Sum of numbers in a given range: C | C++ | Java
- Greatest of two numbers: C | C++ | Java
- Greatest of the Three numbers: C | C++ | Java
- Leap year or not: C | C++ | Java
- Prime number: C | C++ | Java
- Prime number within a given range: C | C++ | Java
- Factorial of a number: C | C++ | Java
- Sum of digits of a number: C | C++ | Java
- Reverse of a number : C | C++ | Java
- Palindrome number: C | C++ | Java
- Armstrong number : C | C++ | Java
- Armstrong number in a given range : C | C++ | Java
- Fibonacci Series upto nth term : C | C++ | Java
- Factorial of a number : C | C++ | Java
- Power of a number : C | C++ | Java
- Factor of a number : C | C++ | Java
- Strong number : C | C++ | Java
- Perfect number : C | C++ | Java
- Automorphic number : C | C++ | Java
- Harshad number : C | C++ | Java
- Abundant number : C| C++ | Java
- Friendly pair : C | C++ | Java


Login/Signup to comment