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

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


この記事では、長方形の周囲を見つける方法を理解します。長方形の周囲長は、長方形のすべての辺の長さを加算して計算されます。

以下は長方形のデモンストレーションです。長方形の周囲は、長方形の2つの長さと2つの幅の全長です-

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

入力

入力が-

であると仮定します
The length of the sides of a rectangle are : 5, 8, 5, 8

出力

必要な出力は-

になります
Perimeter : 26

アルゴリズム

Step 1 – START
Step 2 – Declare 5 floating point variables a, b, c, d and perimeter
Step 3 – Read values of a and b from user as the opposite sides of the rectangle are equal,
only two sides input will be sufficient.
Step 4- Calculate the perimeter by adding up all the sides
Step 5- Display the Perimeter value
Step 6 – STOP

例1

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

import java.util.Scanner;
public class RectanglePerimeter{
   public static void main (String args[]){
      float a ,b, c, d, perimeter;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A Scanner object has been defined ");
      System.out.print("Enter the length of first side : ");
      a = my_scanner.nextFloat();
      System.out.print("Enter the length of second side : ");
      b = my_scanner.nextFloat();
      c = a;
      d = b;
      System.out.printf("The length of the sides of the Rectangle are %.2f %.2f %.2f %.2f", a, b, c, d);
      perimeter = a + b + c + d;
      System.out.printf("\nThe perimeter of Rectangle is: %.2f",perimeter);
   }
}

出力

Required packages have been imported
A Scanner object has been defined
Enter the length of first side : 5
Enter the length of second side : 8
The length of the sides of the Rectangle are 5.00 8.00 5.00 8.00
The perimeter of Rectangle is: 26.00

例2

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

public class RectanglePerimeter{
   public static void main (String args[]){
      float a ,b, c, d, perimeter;
      a=c= 8;
      b=d= 5;
      System.out.printf("The length of the sides of the Rectangle are %.2f %.2f %.2f %.2f", a, b, c, d);
      perimeter = a + b + c + d;
      System.out.printf("\nThe perimeter of Rectangle is: %.2f",perimeter);
   }
}

出力

The length of the sides of the Rectangle are 8.00 5.00 8.00 5.00
The perimeter of Rectangle is: 26.00

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

    この記事では、正方形の面積を見つける方法を理解します。正方形の面積は、次の式を使用して計算されます- side*side i.e. s2 以下は同じのデモンストレーションです- 正方形の辺がsの場合、正方形の面積はs 2で与えられます。 − 入力 入力が-であると仮定します 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. S

  2. Pythonプログラムで円柱の周囲を見つける

    この記事では、以下に示す問題ステートメントの解決策について学習します- 問題の説明 直径と高さを入力し、円柱の周囲長を見つけます。 周囲長は、円柱、つまり長方形の側面図に他なりません。 したがって、周囲長=2 *(h + d) ここで、dは円柱の直径です hは円柱の高さです それでは、実装を見てみましょう 例 # Function to calculate the perimeter of a cylinder def perimeter( diameter, height ) :    return 2 * ( diameter + height )