Calculate the Area of a Circle in Java

0
43

Calculate Area Circle Using Java Example

All programming language is designed to perform a mathematical operation. Java language is bundled with Math library for calculation in a simple and complex problem.

This tutorial will teach you on how your Java program Calculate the Area of a Circle based on user input value. This tutorial will use a Scanner function and an Integer and Double data type for variables. Please follow all the steps to complete this tutorial.

Calculate the Area of a Circle in Java Steps

  1. Create a new class and name it what you want.
How Print Hello World Text in Java, Sum Two Integers Values using Java
How Print Hello World Text in Java

2. Above your Main Class, Insert an import code to access the  Scanner library.

  1. import java.util.Scanner;

3. Declare a scanner and two variables inside your main method.

  1. Scanner input = new Scanner(System.in);
  2. int radius;
  3. double area;

4. After step 3, insert the code below for user input.

  1. System.out.print("Enter value of the circle radius: ");
  2. radius = input.nextInt();

5. After step 4, insert the code below for the algorithm and output.

  1. area = Math.PI * radius * radius;
  2. System.out.println("Area of a circle is: " + area);

6. Run your program and the output should look like the image below.

7. Complete Source Code.

  1. import java.util.Scanner;
  2. public class CalculateCircleArea {
  3. public static void main(String args[]){
  4. Scanner input = new Scanner(System.in);
  5. int radius;
  6. double area;
  7. System.out.print("Enter value of the circle radius: ");
  8. radius = input.nextInt();
  9. area = Math.PI * radius * radius;
  10. System.out.println("Area of a circle is: " + area);
  11. }
  12. }

If you have any comment or suggestions about on How to Calculate the Area of a Circle in Java, feel free to leave your comment below, use the contact page of this website or use my contact information.

Email: info@kmjtech.net  |  Cell. No.: 09468362375

Facebook Comments
(Visited 40 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here