Potencia Usando Funciones

Categorías: Lenguaje C++

   1 // potencia usando funciones
   2 
   3 #include <iostream>
   4 #include <math.h>
   5 using namespace std;
   6 
   7 float potencia(int a, int b){
   8   float pot=1;
   9   if(b==0)
  10     pot=1;
  11   if(b>1){
  12     for (int i=0;i<b;i++)
  13       pot=pot*a;
  14   }
  15   else{
  16     for (int i=0;i<-b;i++)
  17       pot*=a;
  18     pot=1/pot;
  19   }
  20   return pot;
  21 }
  22 
  23 int main (){
  24   int x=0, y=0;
  25   float resultado;
  26   cout <<"la base y el exponente (enteros)\n";
  27   cout <<"a ";
  28   cin >>x;
  29   cout <<"b ";
  30   cin >>y;
  31   resultado=potencia(x,y);
  32   cout<<x <<"^" <<y <<"=" <<resultado <<"\n";
  33   
  34   return 0;  
  35 }

Algoritmantes/PotenciaUsandoFunciones (last edited 2008-04-20 14:39:18 by localhost)