Some Programs Using Math function in Java

Some Programs Using Math function in Java

Some Programs Using Math function in Java



Hi friends, in today's post I shall show you some of the programs you can code by using the Math function of Java. So let's start.

1) Write a program in Java to find the greatest between two numbers.

class Max

{

    public static void main(String args[])

    {

        int a=5, b=9;

        System.out.println("The Greatest between the two numbers is "+ Math.max(a,b));

    }

}

Output: The Greatest between the two numbers is 9

2) Write a program to find the smallest number between the two numbers.

class Min

{

    public static void main(String args[])

    {

        int a=5, b=9;

        System.out.println("The smallest between the two numbers is "+ Math.min(a,b));

    }

}

Output: The smallest between the two numbers is 5

3) Write a program to find the cube root of a number.

class CubeRt

{

    public static void main(String args[])

    {

        int a=9;

        System.out.println("The cube root of the number is "+ Math.cbrt(a));

    }

}

Output: The cube root of the number is 3.0

4) Write a program to find the square root of a number.

class SqRoot

{

    public static void main(String args[])

    {

        int a=4;

        System.out.println("The square root of the number is "+ Math.sqrt(a));

    }

}

Output: The square root of the number is 2.0

One thing I want to tell you all friends, that is these programs that you saw here were only for beginners. There are many more Math functions that I have not shown through these programs. Hope you will understand it and I will also show some more examples in future posts.

Hope you understand the above codes.

Comments