Java Program to Print Pyramid Star Pattern
Last Updated :
29 Dec, 2022
This article will guide you through the process of printing a Pyramid star pattern in Java.
1. Simple pyramid pattern
Java
import java.io.*;
public class GeeksForGeeks {
public static void PyramidStar( int n)
{
int a, b;
for (a = 0 ; a < n; a++) {
for (b = 0 ; b <= a; b++) {
System.out.print( "* " );
}
System.out.println();
}
}
public static void main(String args[])
{
int k = 5 ;
PyramidStar(k);
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
By Recursion
Java
import java.io.*;
class GFG
{
public static void printRow( int n)
{
if (n == 0 )
{
return ;
}
System.out.print( "* " );
printRow(n - 1 );
}
public static void changeRow( int n)
{
if (n == 0 )
{
return ;
}
changeRow(n - 1 );
printRow(n);
System.out.print( "\n" );
}
public static void main (String[] args)
{
GFG.changeRow( 5 );
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
2. After 180 degrees rotation/Mirrored pattern
Here we will print a star pyramid with a rotation of 180 degrees.
Java
import java.io.*;
public class GFG {
public static void FlippedPyramidStar( int k)
{
int a, b;
for (a = 0 ; a < k; a++) {
for (b = 2 * (k - a); b >= 0 ; b--) {
System.out.print( " " );
}
for (b = 0 ; b <= a; b++) {
System.out.print( "* " );
}
System.out.println();
}
}
public static void main(String args[])
{
int k = 5 ;
FlippedPyramidStar(k);
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
3. Printing Triangles:
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
*
* *
* * *
* * * *
* * * * *
4-Star pattern
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
for ( int i= 0 ;i< 5 ;i++)
{
for ( int j= 0 ;j< 5 ;j++)
{
System.out.print( "*" );
}
System.out.println();
}
}
}
|
Output
*****
*****
*****
*****
*****
5- Another different Star pattern
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int n= 5 ;
for ( int i=n;i>= 1 ;i--)
{
for ( int j=i;j>= 1 ;j--)
{
System.out.print( "* " );
}
System.out.println();
}
}
}
|
Output
* * * * *
* * * *
* * *
* *
*
By Recursion
Java
import java.io.*;
class GFG
{
public static void printRow( int n)
{
if (n == 0 )
{
return ;
}
System.out.print( "* " );
printRow(n - 1 );
}
public static void changeRow( int n)
{
if (n == 0 )
{
return ;
}
printRow(n);
System.out.print( "\n" );
changeRow(n - 1 );
}
public static void main (String[] args)
{
GFG.changeRow( 5 );
}
}
|
Output
* * * * *
* * * *
* * *
* *
*
Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...