Java Program to Find Circumference of a Circle

In the previous article, we have seen Java Program to Find Arc Length from Given Angle

In this article we are going to see how to find circumference of a circle using Java programming language.

Java Program to Find Circumference of a Circle

Before Jumping into the program directly let’s see how we can find circumference of a circle.

Explanation:

Let us assume there is a circle with radius “r” 
Then the circumference of the circle is 2*pi*r

Example:

Radius of the circle “r” = 4

Circumference of the circle “C” = 2*pi*r = 2*3.14*4 = 25.12

Where pi = 3.14

Let’s see different ways to find circumference of a circle.

Method-1: Java Program to Find Circumference of a Circle By Using Static Value

Approach:

  • Declare a double variable say ‘r’ and assign the value to it, which holds the value of radius of the circle.
  • Declare a double variable say ‘pi’ and assign the value to it, which holds the value 3.14.
  • Declare a double variable say ‘c’ which will hold the value of circumference of the circle, which we will get by using the formula 2*pi*r
  • Print the result.

Program:

class Main
{
    public static void main(String [] args)
    {
        //raius value declared
        double r = 4;
        //pie value declared
        double pi = 3.14;
        // formula to find circumference of the circle
        double c = 2 * pi * r;
        System.out.println("The circumference of the circle is: " + c);
    }
}

Output:

The circumference of the circle is: 25.12

Method-2: Java Program to Find Circumference of a Circle By Using User Input Value

Approach:

  • Declare a double variable say ‘r’ which holds the value of radius of the circle, take the value of ‘r‘ as user input.
  • Declare a double variable say ‘pi’ which holds the value 3.14.
  • Declare a double variable say ‘c’ which will hold the value of circumference of the circle, which we will get by using the formula 2*pi*r
  • Print the result.

Program:

import java.util.*;

class Main
{
    public static void main(String [] args)
    {
        //Scanner class object created
        Scanner s = new Scanner(System.in); 
        //Taking user input of raius value 
        System.out.println("Enter the value of radius of the circle: ");
        double r = s.nextDouble();

        //pie value declared
        double pi = 3.14;
        // formula to find circumference of the circle
        double c = 2 * pi * r;
        System.out.println("The circumference of the circle is: " + c);
    }
}

Output:

Enter the value of radius of the circle: 
5
The circumference of the circle is: 31.400000000000002

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Articles: