Monday, 14 September 2020

Java program to find the power(m, n) using user-defined function.

In Java, the Math class in java.lang package contains library functions for most of the often-used mathematical functions. Here we will write a program to find the power(m, n) which is similar to the built-in function Math.pow(m, n).


Program

import java.io.*;
//Program to find the power(m,n)
class Power
{
    public static void main(String arg[]) throws IOException
    {
        int pow=1;
        BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter two numbers x and y");
        int m=Integer.parseInt(x.readLine());
        int n=Integer.parseInt(x.readLine());
        for(int i=0;i<n;i++)
        {
          pow=pow*m;
        }
        System.out.println("Power( "+m+" , "+n+" ) = "+pow);
  }
}


OUTPUT
>java Power
Enter two numbers x and y
5
3
Power( 5 , 3 ) = 125

>java Power
Enter two numbers x and y
5
0
Power( 5 , 0 ) = 1

No comments:

Post a Comment