ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

How to calculate Area and Circumference of Circle through a simple Java program ?

Updated on September 17, 2014

Hello Friends,

In this Java tutorial you will learn how to calculate Area and Circumference of a Circle. The following code calculates Area and Circumference of a Circle and displays the result on the console. It is a simple Java program which has a main method. This main method is the first execution point for every simple Java program. The code consists of comments which are self-explanatory. Just copy the code and paste the code on the Eclipse. Run the code and it will prompt you to enter radius of circle. Enter the radius of circle and hit enter key on keyboard to see the output. The output is shown below in the image :

Area and Circumference of the Circle in Java through Scanner.

package com.hubpages.example;

import java.util.Scanner;

public class CircleDemo {

	public static void main(String[] args) {

		// Step 1 --- We will create a Scanner object,
		// it will take System.in object in its constructor
		// This provide Scanner capability of reading
		// user input to console

		Scanner scanner = new Scanner(System.in);

		// Step 2 --- We will ask the user to enter the 
		// value of radius of the circle for which he wants 
		// to calculate the area and circumference of circle

		System.out.println("Enter the Radius of Circle --- ");

		// Step 3 --- User enters radius of circle on the console
		// and it is been read by scanner object by the method 
		// nextDouble() and return value is been stored in the radius
		// variable

		double radius = scanner.nextDouble();

		// Step 4 --- Two variable created for storing the area and 
		// circumference of circle

		double area = 0;
		double circumference = 0;

		System.out.println("Calculating Area and Circumference of Circle ---- ");

		// Step 5 --- Formula for Area of Circle is pi * r * r
		// We have used Math class final variable called as PI
		// which stores the value of pi of Circle
		// Area is calculated and stored in the area variable

		area = Math.PI * radius * radius;

		// Step 6 --- Formula for Circumference of Circle is 2 * pi * r
		// We have used Math class final variable called as PI
		// which stores the value of pi of Circle
		// Circumference is calculated and stored in the circumference variable

		circumference = 2 * Math.PI * radius;

		// Step 7 --- Output is displayed on the console

		System.out.println("The Area of Circle is --- " + area);
		System.out.println("The Circumference of Circle is --- " + circumference);
	}

}

Output of the program ---

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Eunice 3 years ago

      I wish t calculate the Circumference of a circle

    Click to Rate This Article