Wednesday, March 11, 2015

Calculate Circle Area and Circle Perimeter and Rectangle Area in java



Calculate Circle Area

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CalculateCircleArea
{
public static void main(String[] args)
{               
int radius = 0;
System.out.println("Please enter radius of a circle");             
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
radius = Integer.parseInt(br.readLine());
}
catch(NumberFormatException ne)
{
System.out.println("Invalid radius value" + ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}
double area = Math.PI * radius * radius;             
System.out.println("Area of a circle is " + area);
   }
}

Calculate Circle Perimeter

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CalculateCirclePerimeterExample
{
public static void main(String[] args)
{              
int radius = 0;
System.out.println("Please enter radius of a circle");           
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
radius = Integer.parseInt(br.readLine());
}
catch(NumberFormatException ne)
{
System.out.println("Invalid radius value" + ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}
double perimeter = 2 * Math.PI * radius;               
System.out.println("Perimeter of a circle is " + perimeter);
}
}

Calculate Rectangle Area

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CalculateRectArea
          {
public static void main(String[] args)
{               
int width = 0;
int length = 0;                 
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                     
System.out.println("Please enter length of a rectangle");
length = Integer.parseInt(br.readLine());
System.out.println("Please enter width of a rectangle");
width = Integer.parseInt(br.readLine());
catch(NumberFormatException ne)
{
System.out.println("Invalid value" + ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}               
int area = length * width;               
System.out.println("Area of a rectangle is " + area);
   }
}

No comments:

Post a Comment