Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Pattern programming in Java is the practice of creating various shapes or designs by combining symbols or characters. It involves utilizing loops and conditional statements to construct designs like triangles, squares, diamonds, and others.
In programming, pattern problems refer to challenges or exercises that require you to write code that generates a specific pattern of characters or symbols.
Pattern problems in programming can be a fun and challenging way to develop your coding skills, as they require you to think creatively and use a combination of different programming concepts and techniques.
Many people take pattern programs for granted, but it has a high weightage in Interviews to check your ability to think.
In this article, we will be discussing a bunch of different Pattern Programs in Java. So, let us start.
To print patterns in Java, you typically use nested loops. Define the number of rows and columns for the pattern and then use nested loops to iterate through each row and column, printing the desired characters or numbers according to the pattern. Adjust loop conditions and control structures to achieve various pattern designs such as triangles, squares, or other geometric shapes. Experiment with loop patterns and conditional statements to create intricate designs and structures.
The patterns can be classified as :
Star Patterns
Numeric Patterns
Character Patterns
Star Patterns in Java
Star problems pattern questions are a type of programming problem that involves printing out a pattern of stars in a specific shape or formation.
These types of problems can be challenging, but they can also be a fun way to practice your programming skills.
1. Pyramid Program
The Java code for printing a pyramid is given below:
Java
Java
import java.io.*;
public class Ninja { public static void print_pyramid(int n) { // outer loop is used to handle number of rows for (int i=0; i<n; i++) { // inner loop to handles the number of spaces
for (int j=n-i; j>1; j--) { System.out.print(" "); }
//This inner loop is used to handle number of columns for (int j=0; j<=i; j++ ) { System.out.print("* "); } // ending line after each row System.out.println(); } }
public static void main(String args[]) { int n = 5; // Number of rows print_pyramid(n); } }
You can also try this code with Online Java Compiler
The time complexity of the code is O(n2) and the space complexity is O(1).
3. Right Triangle Star Pattern
The Java code for printing a right triangle star pattern is given below:
Java
Java
import java.io.*; public class Ninjas { public static void right_triangle(int n) { int i, j;
// outer loop is used to handle number of rows for(i=0; i<n; i++) { // inner loop is used to handle number of columns for(j=0; j<=i; j++) { System.out.print("* "); } // ending line after each row System.out.println(); } }
public static void main(String args[]) { int n = 5; //Number of Rows right_triangle(n); } }
You can also try this code with Online Java Compiler
The time complexity of the code is O(n2) and the space complexity is O(1).
4. Diamond Shape Pattern Program
The Java code for printing a diamond shape pattern is given below:
Java
Java
import java.util.*;
class Ninja { static void print_Diamond(int n) { int sp = n - 1;
// run loop (parent loop) till number of rows for (int i = 0; i < n; i++) { // loop for initial space for (int j = 0; j < sp; j++) System.out.print(" ");
The time complexity of the code is O(n2) and the space complexity is O(1).
5. Downward Triangle Star Pattern
The Java code for printing a downward triangle star pattern is given below:
Java
Java
import java.util.*;
class Ninja { static void print_Diamond(int n) { int sp = n - 1;
// run loop (parent loop) till number of rows for (int i = 0; i < n; i++) { // loop for initial space for (int j = 0; j < sp; j++) System.out.print(" ");
The time complexity of the code is O(n2) and the space complexity is O(1).
8. Right down Mirror Star Pattern
The Java code for printing a right-down mirror star pattern is given below:
Java
Java
import java.io.*;
public class Ninja { public static void main(String args[]) {
int num=6; //Number of rows //This outer loop will run 6 times as number of rows is 8 for (int i=num; i>=1; i--) { //This inner for loop will print spaces in each row for (int j=num; j>i;j--) { System.out.print(" "); } //This inner for loop will print stars in each row for (int k=1;k<=i;k++) { System.out.print("*"); }
System.out.println(); } } }
You can also try this code with Online Java Compiler
The time complexity of the code is O(n2) and the space complexity is O(1).
9. Right Pascal’s Triangle
The Java code for printing a right Pascal's triangle is given below:
Java
Java
import java.io.*; public class Ninjas { public static void main(String[] args) { int num= 6; //Number of rows
// There are two outer for loops in this program // This is Outer Loop prints the first half of // The Right Pascal triangle pattern for (int i= 0; i<= num-1; i++) { for (int j=0; j<=i; j++) { System.out.print("*"+ " "); } System.out.println(); } //This Outer Loop Prints second half of the triangle for (int i=num-1; i>=0; i--) {
The time complexity of the code is O(n2) and the space complexity is O(1).
10. Left Triangle Pascal’s
The Java code for printing a left Pascal's triangle is given below:
Java
Java
import java.io.*; public class Ninjas { public static void main(String[] args) { int num= 6; //Number of rows
//There are two outer for loops in this program //This Outer Loop prints the first half of // the Left Pascal triangle pattern for (int i= 1; i<=num; i++) {
for (int j=i; j<num; j++) { System.out.print(" "); } //Prints the stars of each row for (int k=1; k<=i;k++) { System.out.print("*"); } System.out.println(); } //This Outer Loop Prints second half of the triangle for (int i=num; i>=1; i--) {
The time complexity of the code is O(n2) and the space complexity is O(1).
11. Sandglass Star Pattern
The Java code for printing a sandglass star pattern is given below:
Java
Java
import java.io.*; public class Ninjas { public static void main(String[] args) { // Number of rows in the first and second half // of Sandglass pattern int num = 5;
//This outer loop Prints the first half of the pattern for (int i= 0; i<= num-1 ; i++) {
for (int j=0; j<i; j++) { System.out.print(" "); } //Prints stars and the whitespaces in between them for (int k=i; k<=num-1; k++) { System.out.print("*" + " "); }
System.out.println(); } //This Outer loop Prints the second half of the pattern for (int i= num-1; i>= 0; i--) {
for (int j=0; j<i; j++) { System.out.print(" "); }
The time complexity of the code is O(n2) and the space complexity is O(1).
15. Diamond Star Pattern
The Java code for printing a diamond star pattern is given below:
Java
Java
import java.util.*;
class Ninja {
static void print_Diamond(int n) { int sp = n - 1;
// run loop (parent loop) till number of rows for (int i = 0; i < n; i++) { // loop for initial space for (int j = 0; j < sp; j++) System.out.print(" ");
The time complexity of the code is O(n2) and the space complexity is O(1).
Numeric Patterns in Java
1. Simple Number Program
The Java code for printing a simple number program is given below:
Java
Java
import java.io.*; public class Ninjas { public static void right_triangle(int n) { int i, j;
// outer loop is used to handle number of rows for(i=0; i<n; i++) { // inner loop is used to handle number of columns for(j=0; j<=i; j++) { System.out.print("1 "); } // ending line after each row System.out.println(); } }
public static void main(String args[]) { int n = 5; //Number of Rows right_triangle(n); } }
You can also try this code with Online Java Compiler
The time complexity of the code is O(n2) and the space complexity is O(1).
2. Number Pattern Program in Java
The Java code for printing a number pattern program is given below:
Java
Java
import java.util.Scanner;
public class Ninjas { public static void main(String[] args) { int rows=5; //Number of Rows // Outer Loop handles the rows for (int i = 1; i <= rows; i++) { // This loop handles the column values for (int j = 1; j <= i; j++) { System.out.print(j+" "); }
System.out.println(); } } }
You can also try this code with Online Java Compiler
The time complexity of the code is O(n2) and the space complexity is O(1).
3. Diamond Pattern Program in Java
The Java code for printing the diamond pattern program is given below:
Java
Java
import java.util.Scanner; class Ninjas { public static void show(int n) { // sp stands for space // st stands for number int sp = n / 2, st = 1;
// This Outer loop for number of lines for (int i = 1; i <= n; i++) {
// This Inner loop for printing space for (int j = 1; j <= sp; j++) System.out.print(" ");
// This Inner loop for printing number int count = st / 2 + 1; for (int k = 1; k <= st; k++) { System.out.print(count); if (k <= st / 2) count--; else count++; }
System.out.println(); if (i <= n / 2) {
sp = sp - 1; st = st + 2; }
else {
sp = sp + 1; st = st - 2; } } }
public static void main(String[] args) { int n=7; show(n); } }
You can also try this code with Online Java Compiler
The time complexity of the code is O(n2) and the space complexity is O(1).
4. Pascal’s Triangle Program in Java
The Java code for printing Pascal’s triangle pattern is given below:
Java
Java
import java.util.Scanner; class Ninjas { public static void show(int n) { // sp stands for space // st stands for number int sp = n / 2, st = 1;
// This Outer loop for number of lines for (int i = 1; i <= n; i++) {
// This Inner loop for printing space for (int j = 1; j <= sp; j++) System.out.print(" ");
// This Inner loop for printing number int count = st / 2 + 1; for (int k = 1; k <= st; k++) { System.out.print(count); if (k <= st / 2) count--; else count++; }
System.out.println(); if (i <= n / 2) {
sp = sp - 1; st = st + 2; }
else {
sp = sp + 1; st = st - 2; } } }
public static void main(String[] args) { int n=7; show(n); } }
You can also try this code with Online Java Compiler
The time complexity of the code is O(n2) and the space complexity is O(1).
5. Number Pattern Program in Java
The Java code for printing a number pattern program is given below:
Java
Java
class Ninjas { public static void main(String args[]) { int n = 5; // Printing Upper Half for n rows // This Loop is to iterate over each row in reverse order for(int i=n;i>=1;i--){ for(int j=1;j<=i;j++){ System.out.print(j+ " "); } System.out.println(); } // Printing Lower Half for n-1 rows // This Loop is to iterate over each row
The Java code for printing a right triangle numeric pattern is given below:
Java
Java
import java.util.Scanner;
public class Ninjas { public static void main(String[] arg) { int rows, i, j; rows = 6;
// This Outer for loop prints one row in every iteration for (i = 1; i <= rows; i++) { // Inner for loop prints numbers of one row for (j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.print("\n"); } } }
You can also try this code with Online Java Compiler
The time complexity of the code is O(n2) and the space complexity is O(1).
10. Diamond Numeric Pattern
The Java code for printing a diamond numeric pattern is given below:
Java
Java
public class Ninjas { public static void main(String[] args) { int row = 5; //Number of rows //Outer Loop to handle number of rows for 1st half for (int i = 1; i <= row; i++) { // This inner loop prints the spaces for (int j = row - i; j >= 1; j--) { System.out.print(" "); }
for (int j = 1; j <= 2 * i - 1; j++) { System.out.print(i); } System.out.println(); } //This outer loop handles number of rows for lower half for (int i = row - 1; i >= 1; i--) { for (int j = row - i; j >= 1; j--) { System.out.print(" "); } for (int j = 1; j <= 2 * i - 1; j++) { System.out.print(i); } System.out.println(); } } }
You can also try this code with Online Java Compiler
The time complexity of the code is O(n2) and the space complexity is O(1).
Character Patterns In Java
Right Alphabetic Triangle
The Java code for printing a right alphabetic triangle is given below:
Java
Java
public class Ninjas { public static void main(String[] args) { int rows = 6; // Number of Rows int alphabet = 65; // ASCII value of the first character
for (int i = 0 ; i < rows; i++ ) { for (int j = 0 ; j <= i; j++ ) { //ASCII value is incremented and the character with that value is printed System.out.print((char)(alphabet + j) + " "); } System.out.println(); } } }
You can also try this code with Online Java Compiler
Pattern programs are simply patterns made up of numbers, alphabets, or symbols in a specific order. These pattern programs are simply solved using loop conditions. Pattern programs are the best way to learn how looping structures function in general-purpose programming languages.
Q. How many types of patterns are there in Java?
In Java, you may construct a variety of patterns, including but not limited to pyramid patterns, number patterns, star patterns, alphabet patterns, and diamond patterns. The possibilities are endless and can be realized by combining loops, conditional expressions, and print statements.
Q. What are the top 3 design patterns in Java?
The Singleton Pattern in Java assures that only one instance of a class exists, the Factory Pattern creates objects without specifying their classes, and the Observer Pattern allows for notice and response to changes in object states.
Q. How to find patterns in Java?
To find patterns in Java, you can use regular expressions (Regex) by importing the java.util.regex package. Define the pattern using Regex syntax and apply methods like matches() or find() to check if the pattern exists in a string or text.
Conclusion
In this article, we have extensively discussed pattern programs in Java. The article explains the different types of pattern programs in Java, namely star patterns, numeric patterns, and character patterns, with their program to print them.
These types of programming questions are very important for interview preparation purposes.
We hope that this blog has helped you enhance your knowledge regarding the pattern programs in Java, and if you would like to learn more, check out our articles on Java, String operations in Java, Regular Expressions in Java, and many more.
Head over to our practice platform Code 360 to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!
Happy Reading!!
Live masterclass
Become a YouTube Analyst: Use Python to analyze viewers data
by Coding Ninjas
04 Feb, 2025
02:30 PM
Get hired as an Amazon SDE : Resume building tips
by Coding Ninjas
03 Feb, 2025
02:30 PM
Expert tips: Ace Leadership roles in Fortune 500 companies
by Coding Ninjas
03 Feb, 2025
12:30 PM
Become a YouTube Analyst: Use Python to analyze viewers data