POTENCIACION MEDIANTE SUMAS SUCESIVAS

En este ejercicio lo que vamos a hacer es elevar un numero a cualquier exponente mediante sumas sucesivas.
A continuacion les dejo el codigo y capturas.

CLASE POTENCIA
package Pckpoten;

public class Potencia {
    private int b;//base
    private int e;//exponente

    public Potencia(int b, int e) {
        this.b = b;
        this.e = e;
    }
   
   
    public int Elevar(){
    int r=0,n=1;
        for(int h=0;h<this.e;h++){
            r=0;
        for(int i=0;i<this.b;i++){
        r+=n;
        }
        n=r;
        }
    return r;
    }   
}

CLASE PRINCIPAL

package Pckpoten;
import java.io.*;
public class Principal {

    public static InputStreamReader Leer= new InputStreamReader(System.in);
    public static BufferedReader Teclado= new BufferedReader(Leer);
    public static void main(String[] args) throws IOException{
        // TODO code application logic here
        int res,a,b;
        System.out.println("Ingrese la base");
        a=Integer.parseInt(Teclado.readLine());
        System.out.println("Ingrese el exponente");
        b=Integer.parseInt(Teclado.readLine());
        Potencia Obj= new Potencia(a,b);
        res=Obj.Elevar();
        System.out.println("El resultado es: "+res);
       
       
    }
}

 
 
 

1 comentarios:

jose alfredo 13 dijo...

podrias mandarmelo pero en lenguaje C++
por favor
gracias

Publicar un comentario