Java
 Computer >> コンピューター >  >> プログラミング >> Java

正方形の領域を見つけるJavaプログラム


この記事では、正方形の面積を見つける方法を理解します。正方形の面積は、次の式を使用して計算されます-

side*side
i.e.
s2

以下は同じのデモンストレーションです-

正方形の辺がsの場合、正方形の面積はs 2 で与えられます。 −

正方形の領域を見つけるJavaプログラム

入力

入力が-

であると仮定します
Length of the side : 4

出力

必要な出力は-

になります
Area of the square : 16

アルゴリズム

Step 1 - START
Step 2 - Declare 2 integer values namely my_side and my_area.
Step 3 - Read the required values from the user/ define the values.
Step 4 – Calculate the area of the square using the formula side*side and store the result
Step 5- Display the result
Step 6- Stop

例1

ここでは、プロンプトに基づいてユーザーが入力を入力しています。この例は、コーディンググラウンドツールでライブで試すことができます 正方形の領域を見つけるJavaプログラム

import java.util.Scanner;
public class AreaOfSquare {
   public static void main(String args[]){
      int my_side, my_area;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the length of a side : ");
      my_side = my_scanner.nextInt();
      my_area = my_side* my_side;
      System.out.println("The my_area of the square is :"+my_area);
   }
}

出力

Required packages have been imported
A reader object has been defined
Enter the length of a side : 4
The area of the square is :16

例2

ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。

public class AreaOfSquare {
   public static void main(String args[]){
      int my_side, my_area;
      my_side = 4;
      System.out.println("The length of the side of the square is defined as : " +my_side);
      my_area = my_side* my_side;
      System.out.println("The my_area of the square is :"+my_area);
   }
}

出力

The length of the side of the square is defined as : 4
The area of the square is :16

  1. 長方形の周囲を見つけるJavaプログラム

    この記事では、長方形の周囲を見つける方法を理解します。長方形の周囲長は、長方形のすべての辺の長さを加算して計算されます。 以下は長方形のデモンストレーションです。長方形の周囲は、長方形の2つの長さと2つの幅の全長です- 入力 入力が-であると仮定します The length of the sides of a rectangle are : 5, 8, 5, 8 出力 必要な出力は-になります Perimeter : 26 アルゴリズム Step 1 – START Step 2 – Declare 5 floating point variabl

  2. Pythonでポリゴンの領域を見つけるプログラム

    順序付けられたポイントのリストが2D平面上の単純なポリゴンエンドポイントを表すとします。このポリゴンの領域を見つける必要があります。 したがって、入力がpoints =[(0、0)、(0,5)、(3、5)、(3,0)]のような場合、出力は15になります。 これを解決するには、次の手順に従います- 関数getInfo()を定義します。これにはx1、y1、x2、y2が必要です return x1 * y2-y1 * x2 メインの方法から、次の手順を実行します N:=ポイントのサイズ (firstx、firsty):=points [0] (prevx、prevy):=(fir