Home > Java > javaTutorial > body text

JAVA program to calculate radius of circle using given width and height of arc

PHPz
Release: 2023-08-21 21:21:57
forward
1288 people have browsed it

JAVA program to calculate radius of circle using given width and height of arc

A circle is a circular two-dimensional figure without corners. Every circle has a starting point, and every point on the circle is equidistant from the starting point. The distance between the starting point and a point on the circle is called the radius of the circle. Similarly, if we draw a line from one side of a circle to the other with the starting point in the middle, that line is called the diameter of the circle. Basically, the diameter is twice the length of the radius.

An arc refers to a part of a circle or a circumference. Simply put, it is an open curve on the circumference of a circle.

According to the problem statement, we need to find the radius of the circle when the width and height of the arc are given.

The formula for calculating the radius of a circle using a given arc width and arc height is −

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

Where, 'r' is the radius of a circle.

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

So, let us go ahead and see how to find the radius of a circle with given arc width and arc height using Java programming language.

Show you some examples−

The Chinese translation of

Instance-1

is:

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
Copy after login
The Chinese translation of

Instance-2

is:

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
Copy after login
The Chinese translation of

Instance-3

is:

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.
Copy after login

grammar

To get any power of a number in Java, we can use the built-in java.lang.Math.pow() method.

The following is the syntax for using the method to obtain the power of 2 -

double power = Math.pow (inputValue,2)
Copy after login

algorithm

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

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

Step 3 - Print the results.

Multiple methods

We provide solutions in different ways.

  • By using static input values.

  • By using user-defined methods.

Let's look at the program and its output one by one.

Method 1: By using static input values

In this method, we declare two double variables to hold the width and height values ​​of the arc and initialize these values ​​in the program. Then by using an algorithm we can find the radius of the circle.

The Chinese translation of

Example

is:

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);
   }
 }
Copy after login

Output

Radius of the circle: 2.541666666666667
Copy after login

Method 2: Use a user-defined method with static input values.

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

The Chinese translation of

Example

is:

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);
   }
}
Copy after login

Output

Radius of the circle: 2.5625
Copy after login

In this article, we explored how to find the radius of a circle in Java, when the width and height of the arc of the circle are known, using different methods.

The above is the detailed content of JAVA program to calculate radius of circle using given width and height of arc. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template