How to calculate a circle perimeter in Java

2 Answers

0 votes
package javaapplication1;

public class JavaApplication1 {
  
    public static void main(String[] args) {
  
        int radius = 30; 
        
        double perimeter = 2 * Math.PI * radius;
               
        System.out.println("Perimeter of a circle is: " + perimeter);
    }
}
  
/*
run:
 
Perimeter of a circle is: 188.49555921538757
  
*/

 



answered Sep 18, 2016 by avibootz
0 votes
package javaapplication1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class JavaApplication1 {
  
    public static void main(String[] args) throws IOException {
  
        int radius = 0; 
        System.out.print("Enter radius: ");
        
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            radius = Integer.parseInt(br.readLine());
        }
        catch(NumberFormatException ne)
        {
            System.out.println("Value Error: " + ne);
            System.exit(0);
        }
        catch(IOException ioe)
        {
            System.out.println("Error:" + ioe);
            System.exit(0);
        }
        
        double perimeter = 2 * Math.PI * radius;
               
        System.out.println("Perimeter of a circle is: " + perimeter);
    }
}
  
/*
run:
 
Enter radius: 45
Perimeter of a circle is: 282.7433388230814
  
*/

 



answered Sep 18, 2016 by avibootz
...