JAVA Program to Calculate Radius of Circle with Given Width and Height of Arc


A circle is a round shape two-dimensional diagram which has no corners. Every circle has an origin point and every point on the circle maintains equal distance from the origin. The distance between the origin and a point in a circle is known as Radius of the circle. And similarly, if we draw a line from one edge to another edge of the circle and the origin is held in the middle of it, that line is known as diameter of the circle. Basically, the diameter is double of the length of the radius.

Arc of the circle refers to a part or portion of the circumference of a circle. In simple terms it is an open curve on the circumference of a circle.

As per the problem statement we have to find the radius of the circle when the width and height of the arc is given.

Formula to find the radius of circle with given width and height of arc −

$$\mathrm{r=w^{2}/8h+h/2}$$

Where, ‘r’ is the radius of a circle.

‘h’ is the height of the arc and ‘w’ is the width of the arc.

So, let's proceed to see how we can find the radius of a circle with a given width and height of arc by using Java programming language.

To show you some instances −

Instance-1

Let say height (h) of arc = 2.
And width of (w) of arc = 4
Radius of circle by using given width and height of arc = 2

Instance-2

Let say height (h) of arc = 3.
And width of (w) of arc = 5
Radius of circle by using given width and height of arc = 2.54

Instance-3

Let say height (h) of arc = 1.
And width of (w) of arc = 4.
Radius of circle by using given width and height of arc = 2.5.

Syntax

To get the power of any number raised to the power of another number in Java we have inbuilt java.lang.Math.pow() method.

Following is the syntax to get power of 2 by using the method −

double power = Math.pow (inputValue,2)

Algorithm

Step-1 − Get the width and height of the arc either by static input or by user input.

Step-2 − Find the radius of the circle by using the formula.

Step-3 − Print the result.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value.

  • By Using User-Defined Method.

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value

In this approach, we declare two double variables to hold the width and height value of arc and initialize the values in the program. Then by using the algorithm we can find theradius of the circle.

Example

public class Main {
   //main method
   public static void main(String[] args) {
      //width (w) and height (h) of arc
      double w = 5, h = 3;
      //find radius using the formula
      double r = ((w * w) / (8 * h) + h / 2);
      System.out.println( "Radius of the circle: "+r);
   }
 }

Output

Radius of the circle: 2.541666666666667

Approach-2: By Using User Defined Method with Static Input Value.

In this approach, we declare two double variables to hold the width and height value of arc and take the values as user input. Then call a user defined method by passing these values as parameters. Then inside the method by using the algorithm we can find the radius of the circle.

Example

public class Main {
   //main method
   public static void main(String[] args) {
      //width (w) and height (h) of arc
      double w = 5, h = 2;
      //call the user defined method to get the radius
      findRadius(w, h);
   }

   //find radius of circle
   static void findRadius(double w, double h) {
      //find radius using the formula
      double r = ((w * w) / (8 * h) + h / 2);
      System.out.println( "Radius of the circle: "+r);
   }
}

Output

Radius of the circle: 2.5625

In this article, we explored how to find the radius of a circle when width and height of arc of the circle is given by using different approaches in Java.

Updated on: 27-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements