Powered by Blogger.

Java Program to find Perimeter of a Circle

>> Saturday, January 30, 2016

The Mathematical equation to find Perimeter or Circumference of a Circle is C=2πr. We can write hte above equation in our program as 2*Math.PI*r
In this tutorial we will learn how to calculate Perimeter(Circumference) of a Circle using java program. To write this program we are using predefined classes like Math, Scanner or BufferedReader. BufferedReader used to read the value of radius at runtime or from console.
Perimeter Of a Circle
Here double java.lang.Math.PI = 3.141592653589793
The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.
Here the Program to calculate area and circumference of circle with user interaction. User will be prompt to enter the radius and the result will be calculated based on the provided radius value.
//program to find Perimeter of a Circle
package com.javabynataraj;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PerimeterOfCircle {
 public static void main(String[] args) {
  int radius = 0;
  double peri;
  //Read the radius of a circle from Console
  System.out.print("Please enter radius of a circle: ");
  try {
   BufferedReader br = new BufferedReader(new InputStreamReader(
     System.in));
   radius = Integer.parseInt(br.readLine());
  }
  // if any invalid value entered
  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);
  }
  peri = 2*Math.PI * radius;
  System.out.println("Perimeter of a circle is: " + peri);
 }
}
Save your java program as PerimeterOfCircle.java in your drive and compile the program using javac -d . PerimeterOfCircle.java

Output:
Output_PerimeterOfCircle

Also find the program to calculate Area of a Cirlce.

Reference Books:

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.