Skip to main content

Patterns: Java Program To Print Patterns Of Numbers And Stars

Patterns are most important for interview point of view. To understand pattern problem you know only the basic tactic of how to solve pattern problem. In Java, it becomes a little bit easier to code patterns problems.

pat11

In this article we discuss 20 basic pattern problem in java.

Java Program: To Check If A Number Is Prime In Java

Lets get start :)

John Academy

Patterns In Java

1. Left Triangle Star Pattern:

First, let us begin with the basic and the commonly asked pattern program in Java i.e This is the basic pattern in java to write its code . Their is only two loops are used one for row and another for column.

pat11

Let’s write the java code:


import java.util.*;
public class Main {

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);

        // write ur code here
        int n = scn.nextInt();
        for (int i = 1; i <= n; i++){
            for(int j = 1; j <= i; j++){
                System.out.print("*\t");
            }
            System.out.println();
        }

    }
}

2. Downward Triangle Star Pattern:

pat21
import java.util.*;
public class Main {
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);

    // write ur code here
    int n = scn.nextInt();
    for (int i = n; i >= 1; i--){
        for(int j = i; j >= 1; j--){
            System.out.print("*\t");
        }
        System.out.println();
    }

}
}

3. Right Triangle Star Pattern:

pat31
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here    
int sp = n - 1;
    int st = 1;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= sp; j++){
            System.out.print("\t");
        }
        for(int k = 1;k <= st; k++){
            System.out.print("*\t");
        }
        System.out.println();
        sp--;
        st++;
    }
}
}

4. Right down Mirror Star Pattern:

pat41
import java.util.*;
public class Main {
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    // write ur code here

    int sp = 0;
    int st = n;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= sp; j++){
            System.out.print("\t");
        }
        for(int k = 1;k <= st; k++){
            System.out.print("*\t");
        }
        System.out.println();
        sp++;
        st--;
    }
}
}

5.  Diamond Shape Pattern Program in Java:

pat51
import java.util.*;
public class Main {
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    // write ur code here

    int sp = n / 2;
    int st = 1;
    for(int i = 1; i <= n; i++){
        for(int j = 1;j <= sp; j++){
            System.out.print("\t");
        }
        for(int k = 1;k <= st; k++){
            System.out.print("*\t");
        }
        if(i <= n / 2){
            sp--;
            st += 2;
        }
        else{
            sp++;
            st -= 2;
        }
        System.out.println();
    }
} }

6. Space Diamond:

pat61
import java.util.*;
public class Main {
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    // write ur code here

    int tst = n + 1;
    int st = tst / 2;
    int sp = 1;
    for(int i = 1; i <= n; i++){
        // System.out.print(st + "," + sp);
        for(int j = 1; j <= st; j++){
            System.out.print("*\t");
        }
        for(int k = 1; k <= sp; k++){
            System.out.print("\t");
        }
        for(int j = 1; j <= st; j++){
            System.out.print("*\t");
        }
        if(i <= n / 2){
        st--;
        sp += 2;
    }
    else{
        st++;
        sp -= 2;
    }
    System.out.println();
    }

} }

7. Left-Diagonal Star Pattern:

pat71
import java.util.*;
public class Main {
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    // write ur code here

    int sp = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < sp; j++){
            System.out.print("\t");
        }
        System.out.print("*");
        System.out.println();
        sp++;
    }
}
}

8. Right-Diagonal Star Pattern:

pat81
import java.util.*;
public class Main {
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    // write ur code here

    int sp = n - 1;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= sp; j++){
            System.out.print("\t");
        }
        System.out.print("*");
        System.out.println();
        sp--;
    }
}
}

9. Double-Diagonal Star Pattern:

pat91
import java.util.*;
public class Main {
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);

    // write ur code here
    int n = scn.nextInt();
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(i+j == n + 1 || i == j){
                System.out.print("*\t");
            }
            else{
                System.out.print("\t");
            }
        }
        System.out.println();
    }

}
}

10. Diamond Star Pattern:

pat101
import java.util.*;
public class Main {
 public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
// write ur code here
 int n = scn.nextInt();
 int os = n / 2;
 int is = -1;
 for(int i = 1; i <= n; i++){
     for(int j = 1; j <= os; j++){
         System.out.print("\t");
     }
     System.out.print("*\t");
     for(int k = 1; k <= is; k++){
         System.out.print("\t");
     }
     if(i > 1 && i < n){
         System.out.print("*\t");
     }
     if(i <= n /2){
         os--;
         is += 2;
     }
     else{
         os++;
         is -= 2;
     }
     System.out.println();
 }
}
}

11. Simple Number Program:

pat111
import java.util.*;

public class Main{
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    // write ur code here
    int var = 1;
    for(int i = 1; i <= n; i++){
        for(int j = 1;j <= i; j++){
            System.out.print(var + "\t");
            var++;
        }
        System.out.println();
    }
}
}

12. Fibonacci Number Pattern:

pat121
import java.util.*;

public class Main{
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    // write ur code here
    int a = 0;
    int b = 1;
    int c;
    for(int i = 1; i <= n; i++){
        for(int j = 1;j <= i; j++){
            System.out.print(a + "\t");
            c = a + b;
            a = b;
            b = c;

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

13. Simple Number Pattern:

pat131
import java.util.*;

public class Main{
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    // write ur code here

    for(int i = 0; i < n; i++){
        int icj = 1;
        for(int j = 0;j <= i; j++){
            System.out.print(icj + "\t");
            int icj1 = icj * (i - j) / (j + 1);
            icj = icj1;
        }
        System.out.println();
    }
}
}

14. Multiplication Number Pattern:

pat141
import java.util.*;

public class Main{
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    // write ur code here

    for(int i = 1; i <= 10; i++){
        int v = n * i;
        System.out.println(n + " * " + i + " = " + v);
    }
}
}

15. Diamond Number Pattern:

pat151
import java.util.*;

public class Main{
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    int sp = n / 2;
    int st = 1;
    int val = 1;
    // write ur code here

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= sp; j++){
            System.out.print("\t");
        }
        int cval = val;
        for(int k = 1; k <= st; k++){
            System.out.print(cval + "\t");
            if (k <= st / 2){
                cval++;
            }
            else{
                cval--;
            }

        }
        if(i <= n /2){
            sp--;
            st += 2;
            val++;
        }
        else{
            sp++;
            st -= 2;
            val--;
        }

        System.out.println();
    }

}
}

16. Half-Space Diamond Number Pattern:

pat161
import java.util.*;

public class Main{

public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
     // write ur code here
     int st = 1;
     int sp = (2 * n) - 3;
     for(int i = 1; i <= n; i++){
         int val = 1;
         for(int j = 1; j <= st; j++){
             System.out.print(val + "\t");
             val++;
         }
         for(int k = 1; k <= sp; k++){
             System.out.print("\t");
         }
         if(i == n){
             st--;
             val--;
         }
         for(int l = 1; l <= st; l++){
             val--;
             System.out.print(val + "\t");
         }
         sp -= 2;
         st++;
         System.out.println();
     }

 }
}

17. Special Cut Diamond Star Pattern:

pat171
import java.util.*;

public class Main{
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);

    // write ur code here
    int n = scn.nextInt();
    int st = 1;
    int sp = n / 2;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= sp; j++){
            if(i == n / 2 + 1){
                System.out.print("*\t");
            }
            else{
            System.out.print("\t");
            }
        }

        for(int j = 1; j <= st; j++){
            System.out.print("*\t");
        }
        if(i <= n/2){
            st++;
        }
        else{
            st--;
        }
        System.out.println();
    }

}
}

18. Sandglass Star Pattern:

pat181


import java.util.*;

public class Main{

public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
     // write ur code here
     int st = n;
     int sp = 0;
     for(int i = 1; i  <= n; i++){
         for(int j = 1; j  <= sp; j++){
             System.out.print("\t");
         }
         for(int j = 1; j <= st; j++){
             if(i > 1 && i  <= n / 2 && j > 1 && j < st){
                 System.out.print("\t");
             }
             else{
             System.out.print("*\t");
             }
         }
         for(int j = 1; j  <= sp; j++){
             System.out.print("\t");
         }
         if(i  <= n/2){
             sp++;
             st -= 2;
         }
         else{
             sp--;
             st +=2;
         }
         System.out.println();
     }

 }
}

19. Swastik Star Pattern:

pat191
import java.util.*;

public class Main{

public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt(); 
// write ur code here
 for(int i = 1; i <= n; i++){
     for(int j = 1; j <= n; j++){
         if(i == 1){
             if(j <= n /2 + 1 || j == n){
                 System.out.print("*\t"); }
             else{
                 System.out.print("\t"); }
         }
         else if(i <= n / 2){
             if(j == n /2 + 1 || j == n){
                 System.out.print("*\t");
             }
             else{
                 System.out.print("\t");
             }

         }
         else if(i == n / 2 + 1){
             System.out.print("*\t");
         }
         else if(i < n){
             if(j == n /2 + 1 || j == 1){
                 System.out.print("*\t");
             }
             else{
                 System.out.print("\t");
             }

         }
         else{
             if(j >= n /2 + 1 || j == 1){
                 System.out.print("*\t");
             }
             else{
                 System.out.print("\t");
             }
         }
     }
     System.out.println();
 }
}
}

20. W Star Pattern:

pat201
import java.util.*;

public class Main {
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    // write ur code here
    int n = scn.nextInt();
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(j == 1 || j == n){
                System.out.print("*\t");
            }
            else if(i > n / 2 && (i == j || i + j == n + 1)){
                System.out.print("*\t");
            }
            else{
                System.out.print("\t");
            }
        }
        System.out.println();
    }

}
}

Comments

Popular posts from this blog

Tic Tac Toe Python 3: The Standard Tic-Tac-Toe Game in Python 3

I just finished Tic tac toe game as my first python project. This is a very simple classic game, for beginners in programming tic-tac-toe game is the best choice. I am using Jupyter notebook, you can use any python ide like Pycharm, Spyder etc. Scenario of tic-tac-toe game: Your task is to write a simple program which pretends to play  Tic-tac-toe game with the user.  To make it easier for you. We have decided to simplify the game. Here are our assumptions. the computer should play a game using 'X' the user should play a game using 'O' the first move belongs to the computer, it always 'X' in the middle of the board.  all squares are numbered row by row, start with '1'. the user move is to enter the number of the square they choose, the number must be valid, that is it must be an integer, it must be greater than  0 and less than 10, and it cannot point to a field which is already occupied. the program checks the ga

Prime Number Program in Java: Print All Prime Numbers

Question You've to print all prime numbers between a range. Take as input "low", the lower limit of the range. Take as input "high", the higher limit of the range. For the range print all the primes numbers between low and high (both included). Input Format low high Output Format n1 n2 .. all primes between low and high (both included) Constraints 2 <= low < high < 10 ^ 6 Sample Input 6 24 Sample Output 7 11 13 17 19 23 Program import java.util.*; public class Main{ public static void main(String[] args) { // write your code here Scanner scn = new Scanner(System.in); int low = scn.nextInt(); int high = scn.nextInt(); for (int i = low; i <= high; i++){ int count = 0; //try to divide n and incrtease count for (int div = 2; div * div <= i; div++){ if ( i % div == 0){ count++;

Fibonacci Java

Question You are given a number n. You are required to print the nth element of the Fibonacci sequence. Note -> Notice precisely how we have defined the Fibonacci sequence 0th element -> 0 1st element -> 1 2nd element -> 1 3rd element -> 2 4th element -> 3 5th element -> 5 6th element -> 8 Input Format A number n Constraints 0 <= n <= 45 Sample Input 10 Sample Output 55 Program import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { // write your code here Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int ans = fib(n, new int[n + 1]); System.out.println(ans); } public static int fib(int n, int[] qb){ if(n == 0 || n == 1){ return n; } if(qb[n] != 0){ return qb[n]; } int fib1 = fib(n - 1, qb);

Knapsack Problem Java

Question You are given a number n, representing the count of items. You are given n numbers, representing the values of n items. You are given n numbers, representing the weights of n items. You are given a number "cap", which is the capacity of a bag you've. You are required to calculate and print the maximum value that can be created in the bag without overflowing its capacity. Note -> Each item can be taken 0 or 1 number of times. You are not allowed to put the same item again and again. Input Format A number n v1 v2 .. n number of elements w1 w2 .. n number of elements A number cap Output Format A number representing the maximum value that can be created in the bag without overflowing its capacity Constraints 1 <= n <= 20 0 <= v1, v2, .. n elements <= 50 0 < w1, w2, .. n elements <= 10 0 < cap <= 10 Sample Input 5 15 14 10 45 30 2 5 1 3 4 7 Sample Output 75 Program import java.io.*;

Merge Sort In Java

Question You are given an array(arr) of integers. You have to sort the given array in increasing order using merge sort. Input Format An Integer n arr1 arr2.. n integers Constraints 1 <= N <= 100000 -10^9 <= arr[i] <= 10^9 Sample Input 5 7 -2 4 1 3 Sample Output Merging these two arrays left array -> 7 right array -> -2 Merging these two arrays left array -> -2 7 right array -> 4 Merging these two arrays left array -> 1 right array -> 3 Merging these two arrays left array -> -2 4 7 right array -> 1 3 Sorted Array -> -2 1 3 4 7 Program import java.io.*; import java.util.*; public class Main { public static int[] mergeSort(int[] arr, int lo, int hi) { //write your code here if(lo == hi){ int[] ba = new int[1]; ba[0] = arr[lo]; return ba; } int mid = (lo + hi) / 2; int[] fsh = mergeSort(arr, lo, mid); int[] ssh = mergeSort(arr, mid + 1, hi); int[]

Count Sort In Java

Question You are given an array(arr) of integers. You have to sort the given array in increasing order using count sort. Input Format An Integer n arr1 arr2.. n integers Constraints 1 <= N <= 10000 0 <= arr[i] <= 10^8 Sample Input 5 7 -2 4 1 3 Sample Output -2 1 3 4 7 Program import java.io.*; import java.util.*; public class Main { public static void countSort(int[] arr, int min, int max) { //write your code here int[] frqa = new int[max - min + 1]; for(int i = 0; i < arr.length; i++){ int val = arr[i]; int pos = val - min; frqa[pos]++; } for(int i = 1; i < frqa.length; i++){ frqa[i] = frqa[i] + frqa[i - 1]; } int[] ans = new int[arr.length]; for(int i = arr.length - 1; i >= 0; i--){ int val = arr[i]; int pos = val - min; int idx = frqa[pos] - 1; ans[idx] = val; frqa[pos]--; } for(int i = 0; i < arr.length;