In this Article, we will learn that how we can calculate power of a number by getting number and exponent power from user in C++.
For example, if Number =2 and Exponent =2 then Result will be 4. Given number will be multiply by itself 2 times and if power becomes 5 then number will be multiply by itself 5 times.
Here is the source code of a Program to calculate Power of a Number enterd by user
Program to calculate Power of Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<iostream> using namespace std; int main() { int number,exponent,i,ans; ans=1; i=1; cout<<"Please Enter a Number : " ; cin>>number; cout<<"Please Enter Exponent : "; cin>>exponent; do { ans=ans*number; i++; } while(i<=exponent); cout<<number<<"^"<<exponent<<"="<<ans; return 0; } |