Finding power of number using java code

Write a java program to find power.(2^4=16)
Finding the pover of one number using another number.
//calculating the power of a given number
import java.io.*;
public class powerofnumber
{
            public static void main(String[]args) throws IOException
              { 
                int num,pow,base;
                BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
                System.out.print("enter the base number:-");
                base = Integer.parseInt(br.readLine());
                System.out.print("enter the power:-");
                pow = Integer.parseInt(br.readLine());
                num = rec(base,pow);
                System.out.println("the number = "+num);
              }
                static int rec(int i,int j)
                {  
                        int r=1;
                        while(j>0)
                        {
                        r=r*i;  
                        j--;
                        }
                     return r;
                }
}

No comments:

Post a Comment