Patterns Program In Java

If you’re preparing for a Java interview and feel intimidated by the pattern Programs questions, don’t worry! They may look daunting at first, but they’re actually based on mathematical logic and matrices fundamentals-two topics you can definitely brush up on before your interview. With a little practice, you’ll be acing these questions in no time.

This Java Pattern Programs article covers a variety of pattern programs that will give you a better understanding of the logic to decode the pattern and become capable of building one in your interview.

Post Type:Java Programs For Beginners
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:Join Our Telegram Group

Patterns Program

I have grouped the programs into the following categories

Star Patterns Program in Java

In this article, we will learn how to print different Star Pattern Programs in Java. This is a popular question for freshers during job interviews, as it tests logical thinking and understanding of flow control. Let’s look at some possible Star Pattern Programs in Java that might help both freshers and experienced Java developers.

Star Pattern Programs In Java
Star Pattern Programs In Java

Star Patterns Program – 1

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class StarPatternsEx1 
{
	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 to print the pattern ");

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

		// Print i number of stars
		for (int i=1; i<=rows; i++)
		{
			for (int j=1; j<=i; j++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
		scanner.close();
	}
}

Star Patterns Program – 2

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class StarPatternsEx2 
{
	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 to print 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; k++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
		scanner.close();
	}
}

Star Patterns Program – 3

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class StarPatternsEx3 
{
	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 to print the pattern ");

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

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

			System.out.println();
		}
		scanner.close();
	}
}

Star Patterns Program – 4

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class StarPatternsEx4 
{
	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 to print the pattern ");

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

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

Pyramid Star Patterns Program in Java

Here are some of the Pyramid Star Pattern in Java which you can face during the interview. So If you are a fresher, before attending the interview don’t forget to go through with these programs of pyramid star pattern in java.

Pyramid Star Pattern Programs in Java
Pyramid Star Pattern Programs in Java

Pyramid Star Patterns Program – 1

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class PyramidStarPatternsEx1 
{
	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 to print 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();
	}
}

Pyramid Star Patterns Program – 2

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class PyramidStarPatternsEx2 
{
	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 to print 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();
	}
}

Diamond Star Patterns Program

Here are some of the Diamond Star Pattern programs in Java.

Diamond Star Pattern Programs In Java
Diamond Star Pattern Programs In Java

Diamond Star Patterns Program Example – 1

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class DiamondStarPatternsEx1 
{
	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 to print 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(); 
		} 
		for (int i=rows-1; i>=1; i--)
		{
			// Print space in increasing order
			for (int j=rows-1; j>=i; j--)
			{
				System.out.print(" ");
			}
			// Print star in decreasing order
			for (int k=1; k<=(i * 2) -1; k++)
			{
				System.out.print("*");
			}

			System.out.println();
		}
		scanner.close();
	}
}

Diamond Star Patterns Program Example – 2

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class DiamondStarPatternsEx2 
{
	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 to print the pattern ");

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

		// Print i number of stars
		for (int i=1; i<=rows; i++)
		{
			for (int j = 1; j <= i; j++)
			{
				System.out.print("*");
			}
			System.out.println();
		}

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

			System.out.println();
		}
		scanner.close();
	}
}

Diamond Star Patterns Program Example – 3

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class DiamondStarPatternsEx3 
{
	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 to print 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; k++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
		for (int i = 1; i <= rows-1; i++)
		{
			// Print space in increasing order
			for (int j = 1; j <= i; j++)
			{
				System.out.print(" ");
			}
			// Print star in decreasing order
			for (int k = rows-1; k >= i; k--)
			{
				System.out.print("*");
			}
			System.out.println();
		}
		scanner.close();
	}
}

Number Patterns Program In Java

One of the important Java interview questions for freshers is number pattern programs in Java. Let’s look at some possible number patterns below.

Number Patterns Program In Java
Number Patterns Program In Java

Number Patterns Program Example – 1

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class NumberPatternEx1 
{
	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 to print 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();
		}
	}
}

Number Patterns Program Example – 2

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class NumberPatternEx2 
{
	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 to print 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(i + " ");
			}
			System.out.println();
		}
	}
}

Number Patterns Program Example – 3

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class NumberPatternEx3 
{
	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 to print 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();
		}
	}
}

Character Patterns Program In Java

Here are the most frequent asked Character Patterns Program In Java.

Character Patterns Program In Java
Character Patterns Program In Java

Character Patterns Example -1

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class CharacterPatternsEx1 
{
	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 to print 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 = 0; j <= i; j++)
			{
				System.out.print((char) (alphabet + j) + " ");
			}
			System.out.println();
		}
	}
}

Character Patterns Example -2

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class CharacterPatternsEx3 
{
	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 to print 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((char) (alphabet + j) + " ");
			}
			System.out.println();
		}
		for (int i = 0; i<= rows; i++)
		{
			int alphabet = 65;
			for (int j = 0; j <= i; j++)
			{
				System.out.print((char) (alphabet + j) + " ");
			}
			System.out.println();
		}
	}
}

Character Patterns Example -3

package com.softwaretestingo.PatternPrograms;
import java.util.Scanner;
public class CharacterPatternsEx3 
{
	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 to print 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((char) (alphabet + j) + " ");
			}
			System.out.println();
		}
		for (int i = 0; i<= rows; i++)
		{
			int alphabet = 65;
			for (int j = 0; j <= i; j++)
			{
				System.out.print((char) (alphabet + j) + " ");
			}
			System.out.println();
		}
	}
}

Conclusion:

Here we have shared most of the Patterns Program In Java which are asked during the interview. But still we can expect some other pattern programs in the interview. If you have facing any other Character, Number & Star Pattern programs in the interview then you can can share those pattern program with us and we will try to update with solution on this blog post. So for update us you can comment in the comment section.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment