aufgabe2c.java 1007 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import java.util.Scanner;
  2. public class aufgabe2c
  3. {
  4. public static int berechnePotenz(int a, int n)
  5. {
  6. /* special case: a^x = 0, if x in N */
  7. if (a == 0)
  8. return 0;
  9. /* special case: x^0 = 1, if x in N and a != 0 */
  10. if (n == 0)
  11. return 1;
  12. int res = 1;
  13. for (int i = 1; i <= n; i++) {
  14. res *= a;
  15. }
  16. return res;
  17. }
  18. public static void main(String[] args)
  19. {
  20. Scanner scanner = new Scanner(System.in);
  21. try {
  22. System.out.printf("Gib einen Wert > 0 fuer a ein: ");
  23. int a = scanner.nextInt();
  24. System.out.printf("Gib einen Wert > 0 fuer n ein: ");
  25. int n = scanner.nextInt();
  26. scanner.close();
  27. int ergebnis = berechnePotenz(a, n);
  28. System.out.println("Das Ergebnis ist: " + ergebnis);
  29. }
  30. catch (Exception ex)
  31. {
  32. System.out.println(ex.toString());
  33. }
  34. }
  35. }