Programs for printing pyramid patterns in Java
Last Updated :
03 May, 2023
This article is aimed at giving a Java implementation for pattern printing.
Simple pyramid pattern
Java
import java.io.*;
public class GeeksForGeeks
{
public static void printStars( int n)
{
int i, j;
for (i= 0 ; i<n; i++)
{
for (j= 0 ; j<=i; j++)
{
System.out.print( "* " );
}
System.out.println();
}
}
public static void main(String args[])
{
int n = 5 ;
printStars(n);
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(n^2)
Space Complexity: O(1)
Method: Using while loop
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int r = 1 , c = 0 , n = 5 ;
while (r <= n) {
while (c <= r - 1 ) {
System.out.print( "* " );
c++;
}
r++;
c = 0 ;
System.out.println();
}
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(N^2),The outer while loop iterates N times, while the inner while loop iterates N-1 times. Hence, the total time complexity of the program will be O(N2).
Space Complexity: O(1),No extra space is required.
Method#2: Using Recursion
Java
import java.io.*;
class GFG {
static void printRow( int num)
{
if (num == 0 )
return ;
System.out.print( "* " );
printRow(num - 1 );
}
static void pattern( int n, int i)
{
if (n == 0 )
return ;
printRow(i);
System.out.println();
pattern(n - 1 , i + 1 );
}
public static void main(String[] args)
{
int n = 5 ;
pattern(n, 1 );
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(N^2)
Auxiliary Space: O(N^2)
After 180 degree rotation
Java
import java.io.*;
public class GeeksForGeeks
{
public static void printStars( int n)
{
int i, j;
for (i= 0 ; i<n; i++)
{
for (j= 2 *(n-i); j>= 0 ; j--)
{
System.out.print( " " );
}
for (j= 0 ; j<=i; j++)
{
System.out.print( "* " );
}
System.out.println();
}
}
public static void main(String args[])
{
int n = 5 ;
printStars(n);
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
The time complexity of the program is O(n^2) because there are two nested loops, each running n times.
The space complexity is O(1).
Method#3: Using Recursion
Java
import java.util.*;
public class GeeksForGeeks
{
static void printSpace( int space)
{
if (space == 0 )
{
return ;
}
System.out.print( " " + " " );
printSpace(space - 1 );
}
static void printStars( int star)
{
if (star == 0 )
{
return ;
}
System.out.print( "* " );
printStars(star - 1 );
}
static void pattern( int n, int num)
{
if (n == 0 )
{
return ;
}
printSpace(n - 1 );
printStars(num - n + 1 );
System.out.println();
pattern(n - 1 , num);
}
public static void main(String args[])
{
int n = 5 ;
pattern(n, n);
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Printing Triangle
Java
import java.io.*;
public class GeeksForGeeks
{
public static void printTriangle( int n)
{
for ( int i= 0 ; i<n; i++)
{
for ( int j=n-i; j> 1 ; j--)
{
System.out.print( " " );
}
for ( int j= 0 ; j<=i; j++ )
{
System.out.print( "* " );
}
System.out.println();
}
}
public static void main(String args[])
{
int n = 5 ;
printTriangle(n);
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Method#4: Using Recursion
Java
import java.util.*;
public class GeeksForGeeks {
static void printSpace( int space)
{
if (space == 0 )
return ;
System.out.print( " " );
printSpace(space - 1 );
}
static void printStar( int asterisk)
{
if (asterisk == 0 )
return ;
System.out.print( "* " );
printStar(asterisk - 1 );
}
static void pattern( int n, int num)
{
if (n == 0 )
return ;
printSpace(n - 1 );
printStar(num - n + 1 );
System.out.println( "" );
pattern(n - 1 , num);
}
public static void main(String[] args)
{
int n = 5 ;
pattern(n, n);
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Print Reverse Of Pyramid
Java
public class ReversePyramid
{
public static void main(String[] args)
{
int rows = 6 ;
for ( int i = 1 ; i <= rows; i++)
{
for ( int j = 1 ; j < i; j++)
{
System.out.print( " " );
}
for ( int j = i; j <= rows; j++)
{
System.out.print(j+ " " );
}
System.out.println();
}
}
}
|
Output
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
Pattern of Number with Mirror Image
Java
public class ReversePattern
{
public static void main(String[] args)
{
int rows = 7 ;
for ( int i = 1 ; i <= rows; i++)
{
for ( int j = 1 ; j < i; j++)
{
System.out.print( " " );
}
for ( int j = i; j <= rows; j++)
{
System.out.print(j+ " " );
}
System.out.println();
}
for ( int i = rows- 1 ; i >= 1 ; i--)
{
for ( int j = 1 ; j < i; j++)
{
System.out.print( " " );
}
for ( int j = i; j <= rows; j++)
{
System.out.print(j+ " " );
}
System.out.println();
}
}
}
|
Output
1 2 3 4 5 6 7
2 3 4 5 6 7
3 4 5 6 7
4 5 6 7
5 6 7
6 7
7
6 7
5 6 7
4 5 6 7
3 4 5 6 7
2 3 4 5 6 7
1 2 3 4 5 6 7
Number Pattern
Java
import java.io.*;
public class GeeksForGeeks
{
public static void printNums( int n)
{
int i, j,num;
for (i= 0 ; i<n; i++)
{
num= 1 ;
for (j= 0 ; j<=i; j++)
{
System.out.print(num+ " " );
num++;
}
System.out.println();
}
}
public static void main(String args[])
{
int n = 5 ;
printNums(n);
}
}
|
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Method5: Using while loop
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int r = 1 , c = 1 , n = 5 ;
while (r <= n) {
while (c <= r ) {
System.out.print(c+ " " );
c++;
}
r++;
c = 1 ;
System.out.println();
}
}
}
|
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Numbers without re assigning
Java
import java.io.*;
public class GeeksForGeeks
{
public static void printNums( int n)
{
int i, j, num= 1 ;
for (i= 0 ; i<n; i++)
{
for (j= 0 ; j<=i; j++)
{
System.out.print(num+ " " );
num = num + 1 ;
}
System.out.println();
}
}
public static void main(String args[])
{
int n = 5 ;
printNums(n);
}
}
|
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Printing Christmas Tree Using Pyramid
Java
class PrintChristmasTree{
public static final int height = 5 ;
public static void main(String[] args) {
int width = 5 ;
int space = width* 5 ;
int x = 1 ;
for ( int a = 1 ;a <= height ;a++){
for ( int i = x;i <= width;i++){
for ( int j = space;j >= i;j--){
System.out.print( " " );
}
for ( int k = 1 ;k <= i;k++){
System.out.print( "* " );
}
System.out.println();
}
x = x+ 2 ;
width = width+ 2 ;
}
for ( int i = 1 ;i <= 4 ;i++){
for ( int j = space- 3 ;j >= 1 ;j--){
System.out.print( " " );
}
for ( int k= 1 ;k <= 4 ;k++){
System.out.print( "* " );
}
System.out.println();
}
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * *
* * * *
* * * *
* * * *
Time complexity: O(h*w*w) for given height h and width w
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...