Day8: Using Math Class

 Using Math Class

  1. In java we have very special predefined class called as Math available in package java.lang.
  2. As the name suggest the class math contains many mathematical constant(called constant as in java as final). and methods to help a programmer for performing mathematical calculation.
  3. One of its very popular member is PI.
  4. It is public, static, double, final data member declared in math class representing the value of PI up to 15 decimal places.
//Write a program to print the value of PI
class Circle
{
public static void main(String [] args)
{
System.out.println(Math.PI);
}
}
Output : 3.141592653589793

//Write a program by using pow and sqrt function of Math class to find 
(i) 21 to the power 2 and 
(ii) square root of 36

class Demo
{
    public static void main(String [] args)
        {
            System.out.println("21 to the power 2 is "+Math.pow(21,2));
            System.out.println("Square root of 36 is "+Math.sqrt(36));
        }
}
Output : Shown in below photo:


//Write a program to calculate hypotenuse using Pythagoras theorem. Assume Perpendicular and base to be integer and initialize them any value of your choice :
Formula : (hypotenuse) = square root of (square of perpendicular + square of base);

class Demo
{
public static void main(String [ ] args)
{
            double hypotenuse,perpendicular, base;
      perpendicular = 5;
base = 4;
hypotenuse = Math.sqrt(Math.pow(perpendicular,2) + Math.pow(base,2));
System.out.println("Hypotenuse is "+hypotenuse);
       }
}
Output : Shown in below photo:


//Write a program o calculate Celsius and Fahrenheit. Assume Fahrenheit to be double and initialize it. With any value of your choice.

class Temp
{
public static void main(String [ ] args)
{
            float cel, far;
     far = 98;
     cel = 5/9 * (far - 32);
     System.out.println("The conversion of Fahrenheit to Celsius of given measurement is "+cel);
       }
} 
Output: Show in photo


Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?