Write a program area of circle in java

import java.util.Scanner;

public class CircleArea {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the radius of the circle: ");
        double radius = scanner.nextDouble();

        // Calculate the area of the circle using the formula: A = π * r^2
        double area = Math.PI * radius * radius;

        System.out.println("The area of the circle is: " + area);

        // Close the scanner
        scanner.close();
    }
}

Output:

Enter the radius of the circle: 5.5
The area of the circle is: 95.03317777109125


This programme includes:

1. To read user input, we import the "java.util.Scanner" class.
2. We construct a "Scanner" object named "scanner" within the "main" function.
3. We ask the user to enter the radius of the circle and scan the "Scanner.nextDouble()" and save the result to the "radius" variable.
4. The area of the circle is calculated using the formula "Math.PI * radius * radius" and stored in the "area" variable.
5. Finally, we use "System.out.println()" to display the computed area.

You can compile and execute the programme to test it. It will ask for the circle's radius, and once you enter it, it will calculate and display the area.

I hope this was helpful! Please let me know if you have any other questions.





Previous Post Next Post