BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Calculate Area and Circumference of Circle

Last Updated: September 15, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, you will learn how to calculate area and circumference of circle in Java. We will see two programs, in the first program, the radius value is initialized in the program and in the second program, the radius value is entered by the user.

Formula for area and circumference of circle

Area of circle = πr2
Circumference of circle = 2πr

Here π value is 22/7 or 3.14 (rounding of the output of 22/7 upto 2 decimal places).
Calculate Area and Circumference of Circle

1. Program to calculate area and circumference of Circle

In this program, radius value is specified in the program and we are calculating the area and circumference using the above mentioned formulas.

You may be wondering why we are using printf() method instead of print() or println(), this is to round of the output upto two decimal points, the format specifier %.2f rounds of the decimal points to two decimal points. If we use the print() method then the program displays the value upto several decimal points, however there is nothing wrong using that as well.

/**
 * @author: BeginnersBook.com
 * @description: Program to calculate area and circumference of
 * circle without user interaction. You need to specify the
 * radius value in program itself.
 */
class JavaExample
{
  public static void main(String args[])
  {
    int radius = 3;
    //formula to calculate area of circle
    double area = Math.PI * (radius * radius);
    System.out.printf("Area is: %.2f", area);

    //formula to calculate circumference of circle
    double circumference= Math.PI * 2*radius;
    System.out.printf( "\nCircumference is: %.2f",circumference) ;
  }
}

Output:
Java Area and Circumference of Circle

2. Radius value is entered by user

In this program, the radius value is entered by the user. We are using Scanner class to capture the user input and store it into a variable. The logic is same as the above program.

/**
 * @author: BeginnersBook.com
 * @description: Program to calculate area and circumference of circle
 * with user interaction. User will be prompted to enter the radius and 
 * the result will be calculated based on the provided radius value.
 */
import java.util.Scanner;
class CircleDemo
{
   static Scanner sc = new Scanner(System.in);
   public static void main(String args[])
   {
      System.out.print("Enter the radius: ");
      /*We are storing the entered radius in double
       * because a user can enter radius in decimals
       */
      double radius = sc.nextDouble();
      //Area = PI*radius*radius
      double area = Math.PI * (radius * radius);
      System.out.println("The area of circle is: " + area);
      //Circumference = 2*PI*radius
      double circumference= Math.PI * 2*radius;
      System.out.println( "The circumference of the circle is:"+circumference) ;
   }
}

Output: As you can see there is a number of digits after decimal point, you can round of this and limit the digits after decimal point using the format specifier in printf() method as shown in the example 1 above.

Enter the radius: 1
The area of circle is: 3.141592653589793
The circumference of the circle is:6.283185307179586

Related Java Programs

  • Java Program to find area of Geometric figures
  • Java Program to calculate area of Rectangle
  • How to print Pattern in Java
  • Java Program to Calculate Simple Interest
❮ Java ProgrammingJava Programs ❯

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program for Decimal to Octal Conversion
  3. Java Program to Add Two Matrix using Multi-dimensional Arrays
  4. Neon Number in Java with example
  5. Java Program for Decimal to Hexadecimal Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. hassan says

    October 26, 2014 at 6:39 PM

    Why r we using static scanner sc? What’s Math.PI?

    Reply
    • Ra's Al Ghul says

      August 24, 2015 at 12:53 AM

      What do you think Math.PI is, you fucking idiot. It’s pi or 3.14.

      Reply
      • hassan says

        December 27, 2015 at 6:52 AM

        Hey asshole!

        It’s good to see you bashing your fabulous vocabulary and knowledge at others. But I was merely confused about the function. So just STFU and get lost instead of swearing at others, you filthy pig. Never asked you for your opinion so stop acting like a fucking retard. Get a life.

        Reply
      • sanny says

        February 28, 2016 at 11:24 PM

        there is no stupid questions, there is only stupid answers.

        Reply
  2. sreekanth says

    October 4, 2015 at 12:27 AM

    1.Write a program to create an integer array that allows user to enter numbers , display the numbers in forward and reverse order then find their sum and average. Also display the element at each index.

    Reply
  3. riya says

    March 8, 2016 at 12:37 PM

    i do not have much programming knowledge.
    can someone tell me the use of following lines?

    1> Scanner sc = new Scanner(System.in);
    why we use system.in ??? what does it mean? and what is it’s use?

    2> double radius = sc.nextDouble();
    Why nextDouble? what does it mean? and what is it’s use?

    Reply
    • Imtiaz Ahmad says

      March 22, 2016 at 6:35 PM

      Hi Dear! Riya
      System.in is an InputStream which is typically connected to keyboard input of console programs. System.in is not used as often since data is commonly passed to a command line Java application via command line arguments, or configuration files. In applications with GUI the input to the application is given via the GUI. This is a separate input mechanism from Java IO.

      Reply
  4. chris says

    April 5, 2016 at 4:51 PM

    How do I round it off

    Reply
  5. Saron says

    March 4, 2017 at 8:42 AM

    Y we shouldn’t call the method can u explain in detail

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap