Java program to calculate the diameter, circumference and area of circle

The program asks the user to enter the radius of a circle and finds the diameter, circumference and area. 
Diameter, area and circumference formulas:
Diameter = 2 * Radius
Area = π * Radius2
Circumference = 2 * π * Radius
We can use the floating-point value 3.1416 for π but in this program we use the Java predefined constant Math.PI.
Area = Math.PI * Radius2;
Circumference = 2 * Math.PI * Radius;
Java program to find diameter, circumference and area of circle:
//diameter, area and circumference of circle 
import java.util.Scanner;
public class JavaCircle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double radius, diameter, circumference, area;

        System.out.print("Enter the radius: ");
        radius = sc.nextDouble();

        diameter = 2 * radius;
        System.out.println("Diameter: " + diameter);

        circumference = 2 * Math.PI * radius;
        System.out.println("Circumference: " + circumference);                                                    

        area = Math.PI * radius * radius;
        System.out.println("Area: " + area);

    }
}
Sample output: :
Enter the radius: 2,5     
Diameter: 5.0
Circumference: 15.707963267948966     
Area: 19.634954084936208
We can use the printf method to display the result with only two decimal places instead of the default decimal places.
This is the statement to display the diameter with two decimal places:
System.out.printf("Diameter: %.2f\n" , diameter);
% indicates that a value will be displayed here, in this case, diameter.
.2 indicates the decimal places to display
f indicates a floating-point value
Java program to find diameter, circumference and area of circle with two decimal places using System.out.printf:
//diameter, area and circumference of circle using System.out.printf
import java.util.Scanner;
public class JavaCircle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double radius, diameter, circumference, area;

        System.out.print("Enter the radius: ");
        radius = sc.nextDouble();

        diameter = 2 * radius;
        System.out.printf("Diameter: %.2f\n" , diameter);

        circumference = 2 * Math.PI * radius;
        System.out.printf("Circumference: %.2f\n" , circumference);                                               

        area = Math.PI * radius * radius;
        System.out.printf("Area: %.2f\n" , area);

    }
}
Sample output: :
Enter the radius: 2,5   
Diameter: 5,00
Circumference: 15,71          
Area: 19,63

1 comment :

  1. MGM Grand Casino - Mapyro
    Find 서귀포 출장샵 all information and reviews 울산광역 출장샵 for MGM 광양 출장마사지 Grand Casino, in Las Vegas at Mapyro. MGM Grand Hotel 거제 출장마사지 and 고양 출장안마 Casino MGM Grand Hotel and Casino.

    ReplyDelete