Advertisement
cesarnascimento

exponenciacao sem math.pow

Feb 23rd, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. package questao1;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Exponenciacao {
  6.  
  7.     public void potencia() {
  8.         Scanner sc = new Scanner(System.in);
  9.         System.out.println("digite a base: ");
  10.         int base = sc.nextInt();
  11.  
  12.         System.out.println("digite o expoente");
  13.         int expoente = sc.nextInt();
  14.         double result = 1;
  15.         for (int i = 0; i < expoente; i++) { //i tem que ser menor que o expoente
  16.             result = result * base; //poderia ser result += *base; iria ser a mesma coisa
  17.         }
  18.         System.out.println(result);
  19.     }
  20. }
  21.  
  22. //main
  23.  
  24. package questao1;
  25.  
  26. public class Main {
  27.  
  28.     public static void main(String[] args) {
  29.         Exponenciacao expo = new Exponenciacao();
  30.         expo.potencia();
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement