7
Power
- #include <iostream>
- template <class T>
- T power( T base, int exp)
- {
- T r = 1;
- while ( exp-- > 0 )
- r *= base;
- return r;
- }
- int main()
- {
- using std::cout;
- using std::endl;
- int i = 5;
- float f = 0.5;
- double d = 22.0/7.0;
- cout << " 5^3 = " << power( i, 3 ) << endl;
- cout << " 5^1 = " << power( i, 1 ) << endl;
- cout << " 5^0 = " << power( i, 0 ) << endl;
- cout << " 0.5^3 = " << power( f, 3 ) << endl;
- cout << "(22/7)^3 = " << power( d, 3 ) << endl;
- }
Comments