This Java code example demonstrates how to calculate the circumference and area of a circle given its radius. The script uses basic mathematical formulas for these calculations and displays the results. This example highlights the use of constants, arithmetic operations, and formatted output in Java.
class CircleCalculations {
public static void main(String[] args) {
// Declare variables
double radius = 7.5;
double circumference, area;
// Calculate circumference
circumference = 2 * Math.PI * radius;
// Calculate area
area = Math.PI * radius * radius;
// Print results
System.out.println("Radius: " + radius);
System.out.println("Circumference: " + circumference);
System.out.println("Area: " + area);
}
}
public static void main(String[] args)
The main method executes the program and contains the logic for calculating the circumference and area of a circle.
public static void main(String[] args) {
// Declare variables
double radius = 7.5;
double circumference, area;
// Calculate circumference
circumference = 2 * Math.PI * radius;
// Calculate area
area = Math.PI * radius * radius;
// Print results
System.out.println("Radius: " + radius);
System.out.println("Circumference: " + circumference);
System.out.println("Area: " + area);
}
double radius = 7.5;
declares and initializes the radius of the circle.double circumference, area;
declares variables to store the circumference and area of the circle.circumference = 2 * Math.PI * radius;
calculates the circumference using the formula C=2πr, where r is the radius of the circle. Math.PI
is a constant provided by the Math
class representing the value of π.area = Math.PI * radius * radius;
calculates the area using the formula A=πr2.System.out.println("Radius: " + radius);
prints the radius of the circle.System.out.println("Circumference: " + circumference);
prints the calculated circumference.System.out.println("Area: " + area);
prints the calculated area.