In this example, i’ll show you How to Calculate power of a number using a for loop in C++.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> using namespace std; int main() { system ("CLS"); //to clear the screen int base,exponent,res; cout<<"Enter value of base: "; cin>>base; cout<<"Enter value of exponent: "; cin>>exponent; long result = 1; for(int i=1;i<=exponent;i++) { result*=base; } cout<<"Result="<<result; return 0; } |
Output: