JAVA program to find the power of a number using method

JAVA program to find the power of a number using method

This JAVA program is to find the power of a number using method.

For example, if base=2 and exponent=4 then power of the number = 16.

Dry run of the program has been given here(click on the link) only additional part is the use method.

We also have an inbuilt java method pow.

As practice you can run the same program with the different ways of using methods.

If you yet need the dry run of the program or any other query, then kindly leave a comment in the comment box or mail me, I would be more than happy to help you.

Program

import java.util.*;

class mr7
{
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
    		System.out.println("Enter base");
    		int base=sc.nextInt();
    		System.out.println("Enter exponent");
    		int exp=sc.nextInt();
		calculate_power(base,exp);
    	}
	
	static void calculate_power(int b,int e)
	{
    		int power=1;
    		while(e>0)
   		{	
        		power=power*b;
        		e--;
    		}
       		System.out.print("Power of the number = "+power);
	}
}

Output

Share Me!