Programs for printing pyramid patterns in Java
This article is aimed at giving a Java implementation for pattern printing.
import java.io.*; // Java code to demonstrate star patterns public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printStars(int n) { int i, j; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // inner loop to handle number of columns // values changing acc. to outer loop for(j=0; j<=i; j++) { // printing stars System.out.print("* "); } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printStars(n); } } |
chevron_right
filter_none
Output:
* * * * * * * * * * * * * * *
import java.io.*; // Java code to demonstrate star pattern public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printStars(int n) { int i, j; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // inner loop to handle number spaces // values changing acc. to requirement for(j=2*(n-i); j>=0; j--) { // printing spaces System.out.print(" "); } // inner loop to handle number of columns // values changing acc. to outer loop for(j=0; j<=i; j++) { // printing stars System.out.print("* "); } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printStars(n); } } |
chevron_right
filter_none
Output:
*
* *
* * *
* * * *
* * * * *
import java.io.*; // Java code to demonstrate star pattern public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printTriagle(int n) { // outer loop to handle number of rows // n in this case for (int i=0; i<n; i++) { // inner loop to handle number spaces // values changing acc. to requirement for (int j=n-i; j>1; j--) { // printing spaces System.out.print(" "); } // inner loop to handle number of columns // values changing acc. to outer loop for (int j=0; j<=i; j++ ) { // printing stars System.out.print("* "); } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printTriagle(n); } } |
chevron_right
filter_none
Output:
*
* *
* * *
* * * *
* * * * *
import java.io.*; // Java code to demonstrate number pattern public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printNums(int n) { int i, j,num; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // initialising starting number num=1; // inner loop to handle number of columns // values changing acc. to outer loop for(j=0; j<=i; j++) { // printing num with a space System.out.print(num+ " "); //incrementing value of num num++; } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printNums(n); } } |
chevron_right
filter_none
Output:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
import java.io.*; // Java code to demonstrate star pattern public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printNums(int n) { // initialising starting number int i, j, num=1; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // without re assigning num // num = 1; for(j=0; j<=i; j++) { // printing num with a space System.out.print(num+ " "); // incrementing num at each column num = num + 1; } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printNums(n); } } |
chevron_right
filter_none
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
This article is contributed by Nikhil Meherwal(S. Shafaq). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Programs for printing pyramid patterns in C++
- PHP programs for printing pyramid patterns
- Programs for printing pyramid patterns using recursion
- Programs for printing pyramid patterns in Python
- Programs to print Interesting Patterns
- Printing Integer between Strings in Java
- Printing Triangle Pattern in Java
- Output of Java Programs | Set 12
- Few Tricky Programs in Java
- How to Compile and Run C/C++/Java Programs in Linux
- Output of Java Programs | Set 14 (Constructors)
- Output of Java programs | Set 13 (Collections)
- Output of Java Programs | Set 54 (Vectors)
- Output of Java programs | Autoboxing and Unboxing
- Output of Java programs | Set 10 (Garbage Collection)
Improved By : GirishKumarPatel



