Print Pattern Java

PROGRAM TO PRINTING PYRAMID PATTERN USING RECURSION



import java.io.*;
  
class MAIN 
{
      
// function to print a row
static void printn(int num)
{
    // base case
    if (num == 0)
        return;
    System.out.print ("* ");
  
    // recursively calling printn()
    printn(num - 1);
}
  
// function to print the pattern
static void pattern(int n, int i)
{
    // base case
    if (n == 0)
        return;
    printn(i);
    System.out.println();
  
    // recursively calling pattern()
    pattern(n - 1, i + 1);
}
  
// Driver code
public static void main (String[] args) 
{
  
    int n = 5;
    pattern(n, 1);
}
}


OUTPUT
* 
* * 
* * * 
* * * * 
* * * * *

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java