正方形の星のパターンを印刷するJavaプログラム
この記事では、正方形の星のパターンを印刷する方法を理解します。パターンは、複数のforループとprintステートメントを使用して形成されます。
以下は同じのデモンストレーションです-
入力
入力が-
であると仮定しますEnter the length of a side : 8
出力
必要な出力は-
になりますThe square pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
アルゴリズム
Step 1 - START Step 2 - Declare three integer values namely i, j and my_input Step 3 - Read the required values from the user/ define the values Step 4 - We iterate through two nested 'for' loops to get space between the characters. Step 5 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the required character. Step 6 - Now, print a newline to get the specific number of characters in the subsequent lines. Step 7 - Display the result Step 8 - Stop
例1
ここでは、8aプロンプトに基づいてユーザーが入力を入力しています。この例は、コーディンググラウンドツールでライブで試すことができます
。
import java.util.Scanner;
public class SquarePattern{
public static void main(String args[]){
int i, j, my_input;
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_input = my_scanner.nextInt();
System.out.println("The square pattern : ");
for(i = 1; i <= my_input; i++){
for(j = 1; j <= my_input; j++){
System.out.print("*");
}
System.out.print("\n");
}
}
} 出力
Required packages have been imported A reader object has been defined Enter the length of a side : 8 The square pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
例2
ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。
public class SquarePattern{
public static void main(String args[]){
int i, j, my_input;
my_input = 8;
System.out.println("The length of a side is defined as " +my_input);
System.out.println("The square pattern : ");
for(i = 1; i <= my_input; i++){
for(j = 1; j <= my_input; j++){
System.out.print("* ");
}
System.out.print("\n");
}
}
} 出力
The length of a side is defined as 8 The square pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-
正方形の領域を見つける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
-
反転した星のパターンを印刷するPythonプログラム
Pythonで逆スターパターンを印刷する必要がある場合は、「for」ループを使用できます。これにより、数値の範囲を反復処理し、必要な文字を必要な頻度で出力できます。反復ごとにカウントを減らすことができます。 以下は同じのデモンストレーションです- 例 N=6 print("The value of 'N' has been initialized to "+str(N)) print("The inverted stars are being displayed") for i in range (N, 0, -1): &