इस लेख में, हम समझेंगे कि डाउनवर्ड ट्राएंगल स्टार पैटर्न को कैसे प्रिंट किया जाए। पैटर्न कई फॉर-लूप और प्रिंट स्टेटमेंट का उपयोग करके बनाया गया है।
नीचे उसी का एक प्रदर्शन है -
इनपुट
मान लीजिए हमारा इनपुट है -
Enter the number of rows : 8
आउटपुट
वांछित आउटपुट होगा -
The downward triangle 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 - 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
यहां, उपयोगकर्ता द्वारा एक प्रॉम्प्ट के आधार पर इनपुट दर्ज किया जा रहा है। आप इस उदाहरण को हमारे कोडिंग ग्राउंड टूल में लाइव देख सकते हैं
।
import java.util.Scanner;
public class DownwardTriangle{
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 of my_input : ");
my_input = my_scanner.nextInt();
System.out.println("The downward triangle star pattern : ");
for ( i= 0; i<= my_input-1; i++){
for ( j=0; j<=i; j++){
System.out.print(" ");
}
for ( k=0; k<=my_input-1-i; k++){
System.out.print("*" + " ");
}
System.out.println();
}
}
} आउटपुट
Required packages have been imported A reader object has been defined Enter the number of my_input : 8 The downward triangle star pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
उदाहरण 2
यहां, पूर्णांक को पहले परिभाषित किया गया है, और इसके मान को एक्सेस किया जाता है और कंसोल पर प्रदर्शित किया जाता है।
public class DownwardTriangle{
public static void main(String args[]){
int i, j, k, my_input;
my_input = 8;
System.out.println("The number of rows is defined as " +my_input);
System.out.println("The downward triangle star pattern : ");
for ( i= 0; i<= my_input-1; i++){
for ( j=0; j<=i; j++){
System.out.print(" ");
}
for ( k=0; k<=my_input-1-i; k++){
System.out.print("*" + " ");
}
System.out.println();
}
}
} आउटपुट
The number of rows is defined as 8
The downward triangle star pattern :
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*