7

Power

  1. #include <iostream>
  2.  
  3. template <class T>
  4. T power( T base, int exp)
  5. {
  6.     T r = 1;
  7.  
  8.     while ( exp-- > 0 )
  9.         r *= base;
  10.  
  11.     return r;
  12. }
  13.  
  14. int main()
  15. {
  16.     using std::cout;
  17.     using std::endl;
  18.  
  19.     int i = 5;
  20.     float f = 0.5;
  21.     double d = 22.0/7.0;
  22.  
  23.     cout << "     5^3 = " << power( i, 3 ) << endl;
  24.     cout << "     5^1 = " << power( i, 1 ) << endl;
  25.     cout << "     5^0 = " << power( i, 0 ) << endl;
  26.     cout << "   0.5^3 = " << power( f, 3 ) << endl;
  27.     cout << "(22/7)^3 = " << power( d, 3 ) << endl;
  28. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].