|
Do you need help with your Java programming?
Click here for instant help with your Java code. |
How to calculate the perimeter of a circle
This example shows how to calculate the perimeter of a circle. First we ask the user for the radius of the circle. The input is parsed to an integer value and finally we calculate the perimeter using the correct formula. |
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * @author javadb.com */ public class Main { /** * Calculates the perimeter of a circle */ public static void main(String[] args) { int radius = 0; System.out.println("Please enter the radius of a circle"); BufferedReader reader = null; try { //get the radius from user input reader = new BufferedReader(new InputStreamReader(System.in)); radius = Integer.parseInt(reader.readLine()); } catch (NumberFormatException nfe) { nfe.printStackTrace(); System.exit(0); } catch (IOException ioe) { ioe.printStackTrace(); System.exit(0); } finally { try { reader.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } double perimeter = 2 * Math.PI * radius; System.out.println("The circle perimeter is " + perimeter); } } |
| Do you know your Java? | |
| Take a Ten-Question-Java-Quiz! | |
Search for code examples on this site
