Table of Contents
To show you some instances −
Instance-3
Algorithm
Multiple Approaches
途径-1:通过使用静态输入值" >途径-1:通过使用静态输入值
Example
Output
Approach-2: By Using User Defined Method with Static Input Value
Approach-3: By Using User Defined Method with User Input Value
Home Java javaTutorial Find the area of ​​a circle inscribed in a square in Java

Find the area of ​​a circle inscribed in a square in Java

Aug 19, 2023 pm 04:53 PM
java area square round cut inside

Find the area of ​​a circle inscribed in a square in Java

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.

A square consists of four sides and all the four sides have equal length. If we try to put a circle inside a square with maximum radius possible, then the diameter of the circle is equal to the length of the side of the square. So here we can conclude that the radius of the circle is equal to half of the square’s side length.

Area of the circle refers to the total surface area acquired by the circle. We can calculate the square of the circle by using radius and a constant known as π

Formula to calculate area of the circle −

$$\mathrm{面积=\pi \times (半径)^{2}}$$

由于圆被内接在一个正方形中,所以圆的半径(r)= 边长/2,其中‘边长’指的是正方形的边长。

$$\mathrm{Area \;of \; inscribed \;circle \;in \;square=\varpi\times(side/2)^{2}=\varpi\times(side^{2}/4)=(\varpi/4)^{*}sides^{2}}$$

在本文中,我们将看到如何找到内接在正方形中的圆的面积

using Java.

To show you some instances −

Instance-1

The side length of the square given = 9
The area of the circle inscribed in square = (ϖ / 4) * side2 = (3.141/4) * 9 * 9 = 63.605
Copy after login

Instance-2

The side length of the square given = 50
The area of the circle inscribed in square = (ϖ / 4) * side2 = (3.141/4) * 50 * 50 =
1963.125
Copy after login

Instance-3

The side length of the square given = 32
The area of the circle inscribed in square = (ϖ / 4) * side2 = (3.141/4) * 32 * 32 = 804.096
Copy after login

Algorithm

Step-1 − Get the side length of the square either by static input or by user input.

Step-2 − Find the area of the circle inscribed in a square by using the formula.

Step-3 − Print the result.

Multiple Approaches

我们以不同的方法提供了解决方案。

  • By Using Static Input Value.

  • 通过使用具有静态输入值的用户定义方法。

  • 通过使用用户定义的方法和用户输入的值。

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

途径-1:通过使用静态输入值

在这种方法中,我们声明一个双精度变量,并将其初始化为正方形的边长。然后通过使用算法,我们可以找到内切于正方形的圆的面积。

Example

import java.io.*;
public class Main {
   //main method
   public static void main (String[] args) {
      //declare a variable to store the value of pi
      double pi = 3.14;
      //declare a variable to store the value of side of the square
      float side = 15;
      //declare a variable to store the area of the circle
      //find area by using the formula
      double area = ( pi / 4 ) * side * side;
      System.out.println("Area of the circle inscribed in the square is: "+ area);
   }
}
Copy after login

Output

Area of the circle inscribed in the square is: 176.625
Copy after login
Copy after login

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

In this approach, we declare a double variable and initialize the side length value of the square. Then by using the algorithm we can find the area of the circle inscribed in a square.

Example

import java.io.*;
public class Main {
   //declare a static variable to store the value of pi
   static double pi = 3.14;

   //main method
   public static void main (String[] args) {
      //declare a variable to store the value of side of the square
      float side = 15;
      System.out.println("Area of the circle inscribed in the square is: "+ areaOfCircle(side));
   }

   // user-defined method to find the area of the circle
   static double areaOfCircle(float side) {
      return ( pi / 4 ) * side * side;
   }
}
Copy after login

Output

Area of the circle inscribed in the square is: 176.625
Copy after login
Copy after login

Approach-3: By Using User Defined Method with User Input Value

在这种方法中,我们声明一个双精度变量,并获取用户输入的正方形的边长。然后通过使用算法,我们可以找到内切于正方形的圆的面积。

Example

import java.io.*;
import java.util.*;
public class Main {
   //declare a static variable to store the value of pi
   static double pi = 3.14;

   //main method
   public static void main (String[] args) {
      //Create object of Scanner class
      Scanner sc= new Scanner(System.in);
      System.out.print("Enter the length of side of the square: ");
      //declare a variable to store the value of side of the square
      double side = sc.nextDouble();
      System.out.println("Area of the circle inscribed in the square is: "+
      areaOfCircle(side));
   }

   // user-defined method to find the area of the circle
   static double areaOfCircle(double side) {
      return ( pi / 4 ) * side * side;
   }
}
Copy after login

Output

Enter the length of side of the square: 9
Area of the circle inscribed in the square is: 63.585
Copy after login

In this article, we explored how to find the area of a circle inscribed in a square in Java by using different approaches.

The above is the detailed content of Find the area of ​​a circle inscribed in a square in Java. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

See all articles