• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

31 Pyramid Program in Java | Pyramid Pattern in Java

June 25, 2019 by javainterviewpoint Leave a Comment

In this article, we will learn to print the different Pyramid Pattern in Java. The pattern programs will help you to master nested loops and recursion in Java. This is one among the popular Java interview questions for fresher. Let’s look into the different Pyramid Program in Java


Pyramid Program in Java

Pattern 1

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern1
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");

        for (int i = 1; i <= rows; i++)
        {
            for (int j = rows; j > i; j--)
            {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++)
            {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    1 
   2 2 
  3 3 3 
 4 4 4 4 
5 5 5 5 5

Pattern 2:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern2
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");

        for (int i = 1; i <= rows; i++)
        {
            for (int j = rows; j > i; j--)
            {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++)
            {
                System.out.print(k + " ");
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5

Pattern 3:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern3
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");

        for (int i = 1; i <= rows; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                System.out.print(" ");
            }
            for (int k = rows; k >= i; k--)
            {
                System.out.print(k + " ");
            }

            System.out.println();
        }
    }
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
 5 4 3 2 1 
  5 4 3 2 
   5 4 3 
    5 4 
     5 

Pattern 4:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern4
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();

		System.out.println("** Printing the pattern... **");

		for (int i = rows; i >= 1; i--)
		{
			for (int j = i; j >= 1; j--)
			{
				System.out.print(" ");
			}
			for (int k = rows; k >=i; k--)
			{
				System.out.print(k + " ");
			}
			System.out.println();
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
     5 
    5 4 
   5 4 3 
  5 4 3 2 
 5 4 3 2 1

Pattern 5:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern5
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();

		System.out.println("** Printing the pattern... **");

		for (int i = 1; i <= rows; i++)
		{
			for (int j = 1; j <= i ; j++)
			{
				System.out.print(" ");
			}
			for (int k = 1; k <= rows-i+1; k++)
			{
				System.out.print(k + " ");
			}
			
			System.out.println();
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
 1 2 3 4 5 
  1 2 3 4 
   1 2 3 
    1 2 
     1

Pattern 6:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern6
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");

        int count = 1;
        for (int i = 1; i <= rows; i++)
        {
            for (int j = rows; j > i; j--)
            {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++)
            {
                System.out.print(count + " ");
                count++;
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    1 
   2 3 
  4 5 6 
 7 8 9 10 
11 12 13 14 15

Pattern 7:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern7
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");

        for (int i = 1; i <= rows; i++)
        {
            for (int j = rows; j > i; j--)
            {
                System.out.print(" ");
            }
            for (int k = i; k >= 1; k--)
            {
                System.out.print(k + " ");
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    1 
   2 1 
  3 2 1 
 4 3 2 1 
5 4 3 2 1 

Pattern 8:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern8
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();

		System.out.println("** Printing the pattern... **");

		for (int i = 1; i <= rows; i++)
		{
			for (int j = rows; j > i; j--)
			{
				System.out.print(" ");
			}
			int temp = i; 
			for (int j = i; j >= 1; j--) 
			{ 
				System.out.print(temp + " "); 
				temp = temp + rows; 
			}
			System.out.println();
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    1 
   2 7 
  3 8 13 
 4 9 14 19 
5 10 15 20 25

Pattern 9:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern9
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();

		System.out.println("** Printing the pattern... **");

		for (int i = 1; i <= rows; i++)
		{
			for (int j = rows; j > i; j--)
			{
				System.out.print(" ");
			}
			int temp = 1; 
			for (int k = 1; k <= i; k++) 
			{ 
				System.out.print(temp + " "); 
				temp = temp * (i - k) / (k);
			}
			System.out.println();
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    1 
   1 1 
  1 2 1 
 1 3 3 1 
1 4 6 4 1 

Pattern 10:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern10
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();

		System.out.println("** Printing the pattern... **");

		for (int i = rows; i >= 1; i--) 
		{ 
			for (int j = 1; j < i; j++) 
			{ 
				System.out.print(" "); 
			}
			for (int k = i; k <= rows; k++) 
			{ 
				System.out.print(k + " "); 
			} 
			System.out.println(); 
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    5 
   4 5 
  3 4 5 
 2 3 4 5 
1 2 3 4 5

Pattern 11:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern11
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");

        for (int i = 1; i <= rows; i++)
        {
            for (int j = rows; j > i; j--)
            {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++)
            {
                System.out.print(k);
            }
            for (int l = i - 1; l >= 1; l--)
            {
                System.out.print(l);
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    1
   121
  12321
 1234321
123454321

Pattern 12:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern12
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();

		System.out.println("** Printing the pattern... **");
		
		for (int i = 1; i <= rows; i++)
		{
			for (int j = rows; j > i; j--)
			{
				System.out.print(" ");
			}
			for (int k = 1; k <= i; k++)
			{
				System.out.print( k % 2 + " ");
			}
			System.out.println();
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    1 
   1 0 
  1 0 1 
 1 0 1 0 
1 0 1 0 1 

Pattern 13:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern13
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();

		System.out.println("** Printing the pattern... **");

		for (int i = 1; i <= rows; i++)
		{
			for (int j = rows; j > i; j--)
			{
				System.out.print(" ");
			}
			int temp = i;
			for (int k = 1; k <= i; k++)
			{
				System.out.print(temp + " ");
				temp = temp + rows- k;
			}
			System.out.println();
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    1 
   2 6 
  3 7 10 
 4 8 11 13 
5 9 12 14 15

Pattern 14:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern14
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();

		System.out.println("** Printing the pattern... **");

		for (int i = 0; i < rows; i++) 
		{ 
			for (int j = rows; j > i; j--)
			{
				System.out.print(" ");
			}
			for (int k = 0; k <= i; k++) 
			{ 
				if (k % 2 == 0) 
				{ 
					System.out.print(1 + k * rows - (k - 1) * k / 2 + i - k + " "); 
				} 
				else 
				{ 
					System.out.print(1 + k * rows - (k - 1) * k / 2 + rows - 1 - i + " "); 
				} 
			} 
			System.out.println(); 
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
     1 
    2 9 
   3 8 10 
  4 7 11 14 
 5 6 12 13 15 

Pattern 15:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern15
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();

		System.out.println("** Printing the pattern... **");

		int temp = 1; 
		for(int i = 1; i <= rows/2+1; i++) 
		{ 
			for(int j = 1;j <= i;j++) 
			{ 
				System.out.print(temp*j+" "); 
			} 
				System.out.println(); 
				temp++; 
		} 
		for(int i = 1; i <= rows/2; i++) 
		{ 
			for(int j = 1;j <= rows/2+1-i; j++) 
			{ 
				System.out.print(temp*j+" "); 
			} 
			System.out.println(); 
			temp++; 
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
6
** Printing the pattern... **
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 
6 12 
7

Pattern 16:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern16
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");

        for (int i = 1; i <= rows; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                System.out.print(j + " ");
            }
            System.out.println();
        }
        for (int i = rows; i >= 1; i--)
        {
            for (int j = 1; j < i; j++)
            {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1

Pattern 17:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern17
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");

        for (int i = rows; i >= 1; i--)
        {
            for (int j = i; j <= rows; j++)
            {
                System.out.print(j + " ");
            }
            for (int k = rows - 1; k >= i; k--)
            {
                System.out.print(k + " ");
            }
            System.out.println();
        }
        for (int i = 2; i <= rows; i++)
        {
            for (int j = i; j <= rows; j++)
            {
                System.out.print(j + " ");
            }
            for (int k = rows - 1; k >= i; k--)
            {
                System.out.print(k + " ");
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
5 
4 5 4 
3 4 5 4 3 
2 3 4 5 4 3 2 
1 2 3 4 5 4 3 2 1 
2 3 4 5 4 3 2 
3 4 5 4 3 
4 5 4 
5 

Pattern 18:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern18
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");

        for (int i = 0; i <= rows; i++)
        {
            int alphabet = 65;
            for (int j = rows; j > i; j--)
            {
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++)
            {
                System.out.print((char) (alphabet + k) + " ");
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows needed in the pattern 
4
** Printing the pattern... **     
    A 
   A B 
  A B C 
 A B C D 
A B C D E 

Pattern 19:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern19
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();
		
		System.out.println("** Printing the pattern... **");
		
		for (int i = 0; i <= rows; i++)
		{
			int alphabet = 65;
			for (int j = rows; j > i; j--)
			{
				System.out.print(" ");
			}
			for (int k = 0; k <= i; k++)
			{
				System.out.print((char) (alphabet + i) + " ");
			}
			System.out.println();
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
4
** Printing the pattern... **
    A 
   B B 
  C C C 
 D D D D 
E E E E E

Pattern 20:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern20
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();
		
		System.out.println("** Printing the pattern... **");
		
		for (int i = rows; i >= 0; i--) 
		{ 
			int alphabet = 65; 
			for (int j = 0; j < i; j++) 
			{ 
				System.out.print(" "); 
			} 
			for (int k = i; k <= rows; k++) 
			{ 
				System.out.print((char) (alphabet + k) + " "); 
			} 
			System.out.println(); 
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
4
** Printing the pattern... **
    E 
   D E 
  C D E 
 B C D E 
A B C D E

Pattern 21:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern21
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();
		
		System.out.println("** Printing the pattern... **");
		
		for (int i = 0; i <= rows; i++) 
		{ 
			int alphabet = 65; 
			for (int j = 0; j < i; j++) 
			{ 
				System.out.print(" "); 
			} 
			for (int k = i; k <= rows; k++) 
			{ 
				System.out.print((char) (alphabet + k) + " "); 
			}
			System.out.println(); 
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
4
** Printing the pattern... **
A B C D E 
 B C D E 
  C D E 
   D E 
    E

Pattern 22:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern22
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();
		
		System.out.println("** Printing the pattern... **");
		
		for (int i = 0; i <= rows; i++) 
		{ 
			int alphabet = 65; 
			for (int j = rows; j > i; j--) 
			{ 
				System.out.print(" "); 
			} 
			for (int k = 0; k <= i; k++) 
			{ 
				System.out.print((char) (alphabet + k)); 
			} 
			for (int l = i - 1; l >= 0; l--) 
			{ 
				System.out.print((char) (alphabet + l)); 
			} 
			System.out.println(); 
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
4
** Printing the pattern... **
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA

Pattern 23:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern23
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();
		
		System.out.println("** Printing the pattern... **");
		
		for (int i = 0; i <= 5; i++) 
		{ 
			int alphabet = 65; 
			for (int j = 0; j <= i; j++) 
			{ 
				System.out.print(" "); 
			} 
			for (int k = 0; k <= rows - i; k++) 
			{ 
				System.out.print((char) (alphabet + k) + " "); 
			} 
			System.out.println(); 
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
4
** Printing the pattern... **
 A B C D E 
  A B C D 
   A B C 
    A B 
     A 

Pattern 24:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern24
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");
        int alphabet = 65;

        for (int i = 0; i <= rows; i++)
        {
            for (int j = rows; j > i; j--)
            {
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++)
            {
                System.out.print((char) alphabet + " ");
                alphabet++;
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows needed in the pattern 
4
** Printing the pattern... **
    A 
   B C 
  D E F 
 G H I J 
K L M N O

Pattern 25:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern25
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();
		
		System.out.println("** Printing the pattern... **");
		
		for (int i = 0; i <= rows; i++) 
		{ 
			for (int j = rows; j > i; j--)
			{
				System.out.print(" ");
			}
			int alphabet = 65; 
			for (int k = i; k >= 0; k--) 
			{ 
				System.out.print((char) (alphabet + k) + " "); 
			}
			System.out.println(); 
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
4
** Printing the pattern... **
    A 
   B A 
  C B A 
 D C B A 
E D C B A 

Pattern 26:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern26
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();
		
		int currentRow = 1; 
		int counter = 1; 
		int alphabet = 65;
		
		System.out.println("** Printing the pattern... **");
		
		for (int i=1; i<= rows; i++) 
		{ 
			for (int j = rows; j > i; j--)
			{
				System.out.print(" ");
			}
			if (i % 2 == 0) 
			{ 
				int reverse = currentRow + counter - 1; 
				for (int k = 0; k<i; k++) 
				{ 
					System.out.print((char) (reverse + alphabet -1)+" "); 
					reverse--; 
					counter++; 
				} 
			} 
			else 
			{ 
				for (int k = 1; k<=i; k++) 
				{ 
					System.out.print((char) (counter + alphabet - 1) +" "); 
					counter++; 
				} 
			} 
			System.out.println(); 
			currentRow++; 
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    A 
   C B 
  D E F 
 J I H G 
K L M N O

Pattern 27:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern27
{
	public static void main(String[] args)
	{
		// Create a new Scanner object
		Scanner scanner = new Scanner(System.in);

		// Get the number of rows from the user
		System.out.println("Enter the number of rows needed in the pattern ");

		int rows = scanner.nextInt();
		
		System.out.println("** Printing the pattern... **");
		
		for (int i = 1; i <= rows; i++) 
		{ 
			int alphabet = 65; 
			for (int j = rows; j > i; j--) 
			{ 
				System.out.print(" "); 
			} 
			int temp = 1; 
			for (int k = 1; k <= i; k++)
			{ 
				System.out.print((char) (alphabet - 1 + temp) + " "); 
				temp = temp * (i - k) / (k); 
			} 
			System.out.println(); 
		}
	}
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
    A 
   A A 
  A B A 
 A C C A 
A D F D A

Pattern 28:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern28
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();
        System.out.println("## Printing the pattern ##");

        for (int i = 1; i <= rows; i++)
        {
            // Print space in decreasing order
            for (int j = rows; j > i; j--)
            {
                System.out.print(" ");
            }
            // Print star in increasing order
            for (int k = 1; k <= (i * 2) - 1; k++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
        scanner.close();
    }
}

Output:

Enter the number of rows needed in the pattern 
5
## Printing the pattern ##
    *
   ***
  *****
 *******
*********

Pattern 29:

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern29
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();
        
        System.out.println("## Printing the pattern ##");
        
        for (int i=rows; i>=1; i--)
        {
            // Print star in decreasing order
            for (int k=1; k<=(i * 2) -1; k++) 
            { 
                System.out.print("*"); 
            } 
            System.out.println(); 
            // Print space in increasing order 
            for (int j=rows; j>=i; j--)
            {
                System.out.print(" ");
            }
            
        }
        scanner.close();
    }
}

Output:

Enter the number of rows needed in the pattern 
5
## Printing the pattern ##
*********
 *******
  *****
   ***
    *

Pattern 30

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern30
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");

        for (int i = 1; i <= rows; i++) 
        { 
            for (int j = 1; j <= i; j++) 
            { 
                System.out.print("* "); 
            } 
            System.out.println(); 
        } 
        for (int i = rows; i >= 1; i--) 
        { 
            for (int j = 1; j < i; j++) 
            { 
                System.out.print("* "); 
            } 
            System.out.println(); 
        }
    }
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
*

Pattern 31:

import java.util.Scanner;

public class Pattern31
{
    public static void main(String[] args)
    {
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);

        // Get the number of rows from the user
        System.out.println("Enter the number of rows needed in the pattern ");

        int rows = scanner.nextInt();

        System.out.println("** Printing the pattern... **");

        for (int i = rows; i >= 1; i--) 
        { 
            for (int j = i; j <= rows; j++) 
            { 
                System.out.print("* "); 
            } 
            for (int k = rows-1; k >= i; k--) 
            { 
                System.out.print("* "); 
            } 
            System.out.println(); 
        } 
        for (int i = 2; i <= rows; i++) 
        { 
            for (int j = i; j <= rows; j++) 
            { 
                System.out.print("* "); 
            } 
            for (int k = rows-1; k >= i; k--)
            { 
                System.out.print("* "); 
            } 
            System.out.println(); 
        } 
    }
}

Output:

Enter the number of rows needed in the pattern 
5
** Printing the pattern... **
* 
* * * 
* * * * * 
* * * * * * * 
* * * * * * * * * 
* * * * * * * 
* * * * * 
* * * 
*

Filed Under: Java, Java Interview Tagged With: Diamond pattern, Half Diamond Pattern, Inverted Pyramid, Java Pyramid Pattern, Java Pyramid Pattern Program, Java Pyramid Program, Pyramid pattern, Right Triangle Pattern, Star Pattern, Triangle Pattern

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·