/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javaapplication2; /** * * @author zzaier */ public class JavaApplication2 { /** * @param args the command line arguments */ public static void main(String[] args) { double resultPower=printPower(5,3); System.out.println("result of 5 power 3 is " +resultPower); resultPower=printPower(5,-3); System.out.println("result of 5 power -3 is " +resultPower); } static public double printPower(int base, int exponent) { double result = 1; //you can also use Math.abs // int absExponent =Math.abs(exponent); int absExponent; if (exponent < 0) { absExponent = -exponent; } else { absExponent = exponent; } for (int i = 0; i < absExponent; i++) { result *= base; } if (exponent < 0) { return 1 / result; } else { return result; } } }