总结
豆包 AI 助手文章总结
首页 > Java > java教程 > 正文

在Java中找到一个内切于正方形的圆的面积

王林
发布: 2023-08-19 16:53:20
转载
1019人浏览过

在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 −

立即学习Java免费学习笔记(深入)”;

$$\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
登录后复制

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
登录后复制

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
登录后复制

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);
   }
}
登录后复制

Output

Area of the circle inscribed in the square is: 176.625
登录后复制
登录后复制

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;
   }
}
登录后复制

Output

Area of the circle inscribed in the square is: 176.625
登录后复制
登录后复制

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;
   }
}
登录后复制

Output

Enter the length of side of the square: 9
Area of the circle inscribed in the square is: 63.585
登录后复制

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

以上就是在Java中找到一个内切于正方形的圆的面积的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号