ダイヤモンドスターパターンを印刷するJavaプログラム
この記事では、ダイヤモンドスターパターンを印刷する方法を理解します。パターンは、複数のforループとprintステートメントを使用して形成されます。
以下は同じのデモンストレーションです-
入力
入力が-
であると仮定しますEnter the number of rows : 8
出力
必要な出力は-
になりますThe diamond star pattern : * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** *
アルゴリズム
Step 1 - START Step 2 - Declare four integer values namely i, j, k and my_input. Step 3 - Read the required values from the user/ define the values Step 4 - Assign value of ‘my_input – 1’ to ‘k’ Step 5 - We iterate through two nested 'for' loops to get space between the characters. Step 6 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the required character. Step 7 - Now, print a newline to get the specific number of characters in the subsequent lines. Step 8 - Display the result Step 9 - Stop
例1
ここでは、プロンプトに基づいてユーザーが入力を入力しています。この例は、コーディンググラウンドツールでライブで試すことができます
。
import java.util.Scanner;
public class DiamondStar{
public static void main(String args[]){
int i, j, k, 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 number : ");
my_input = my_scanner.nextInt();
k = my_input - 1;
System.out.println("The diamond star pattern : ");
for (j = 1; j<= my_input; j++){
for (i = 1; i<= k; i++){
System.out.print(" ");
}
k--;
for (i = 1; i <= 2 * j - 1; i++){
System.out.print("*");
}
System.out.println("");
}
k = 1;
for (j = 1; j<= my_input - 1; j++){
for (i = 1; i<= k; i++){
System.out.print(" ");
}
k++;
for (i = 1; i<= 2 * (my_input - j) - 1; i++){
System.out.print("*");
}
System.out.println("");
}
}
} 出力
Required packages have been imported
A reader object has been defined
Enter the number : 8
The diamond star pattern :
*
***
*****
*******
*********
***********
*************
***************
*************
***********
*********
*******
*****
***
* 例2
ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。
public class DiamondStar{
public static void main(String args[]){
int i, j, k, my_input;
my_input = 8;
k = my_input - 1;
System.out.println("The number of rows is defined as " +my_input);
System.out.println("The diamond star pattern : ");
for (j = 1; j<= my_input; j++){
for (i = 1; i<= k; i++){
System.out.print(" ");
}
k--;
for (i = 1; i <= 2 * j - 1; i++){
System.out.print("*");
}
System.out.println("");
}
k = 1;
for (j = 1; j<= my_input - 1; j++){
for (i = 1; i<= k; i++){
System.out.print(" ");
}
k++;
for (i = 1; i<= 2 * (my_input - j) - 1; i++){
System.out.print("*");
}
System.out.println("");
}
}
} 出力
The number of rows is defined as 8
The diamond star pattern :
*
***
*****
*******
*********
***********
*************
***************
*************
***********
*********
*******
*****
***
* -
中空の長方形の星のパターンを印刷するCプログラム
ここでは、Cプログラミング言語のforループを使用して、中空の長方形のスター(*)パターンを印刷します。 以下に示す例を考えてみましょう- 入力 Enter number of rows: 5 出力 出力は次のとおりです- ***** * * * * * * ***** アルゴリズム forループを使用して中空の長方形のstar(*)パターンを印刷する方法を説明するアルゴリズムを以下に示します。 ステップ1 −実行時に印刷する行数を入力します。 ステップ2 −1からNまでの行にouterforループ
-
Cでダイヤモンドパターンを印刷するプログラム
プログラムの説明 ダイアモンドパターンは、単純なピラミッドパターンと逆ピラミッドパターンを組み合わせたものです。 アルゴリズム First Row: Display 1 Second Row: Display 1,2,3 Third Row: Display 1,2,3,4,5 Fourth Row: Display 1,2,3,4,5,6,7 Fifth Row: Display 1,2,3,4,5,6,7,8,9 Display the same contents from 4th Row till First Row below the fifth Row. 例 /* Program