C/Math/Power

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Calculate numeric power: how to use pow

<source lang="cpp">

  1. include <stdio.h>
  2. include <math.h>

int main () {

 printf ("7 ^ 3 = %lf\n", pow ( 7 , 3 ) );
 printf ("5.73 ^ 12 = %lf\n", pow (5.73, 12 ) );
 printf ("31.01 ^ 1.54 = %lf\n", pow (31.01, 1.54));
 return 0;

}


      </source>


How to use pow

<source lang="cpp">

  1. include <math.h>
  2. include <stdio.h>

int main(void) {

 double x = 10.0, y = 0.0;
 do {
   printf("%f\n", pow(x, y));
   y++;
 } while(y<11.0);
 return 0;

}


      </source>


Our own power function

<source lang="cpp">

  1. include <stdio.h>

int power(void); int m, e; int main(void) {

 m = 2;
 e = 3;
 printf("%d raised to the %d power = %d", m, e, power());
 return 0;

} int power(void) {

 int temp, temp2;
 temp = 1;
 temp2 = e;
 for( ; temp2> 0; temp2--) 
     temp = temp * m;
 return temp;

}


      </source>