Pattern Exploration for 10 Days Success!

We have successfully completed 10 days in our 100 days Coding journey on this most Special Day of 2011 - our ICT lifted the World Cup after 28 loooong years under Captain Cool's Captaincy! Kudos to us as well as our Indian team! :)

So, here are 10 pattern program for practice! 

1. Triangle Pattern

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *

Python

def trianglePattern(n):

    for i in range(1, n + 1):

        print("* " * i)

trianglePattern(7)


Java

public class TrianglePattern {

    public static void main(String[] args) {

        int n = 7;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= i; j++) {

                System.out.print("* ");

            }

            System.out.println();

        }

    }

}


2. Pyramid Pattern

      *
     ***
    *****
   *******
  *********
 ***********
*************

Python

def pyramidPattern(n):

    for i in range(0, n):

        print(" " * (n - i - 1) + "*" * (2 * i + 1))

pyramidPattern(7)


Java

public class PyramidPattern {

    public static void main(String[] args) {

        int n = 7;

        for (int i = 1; i <= n; i++) {

            for (int j = i; j < n; j++) {

                System.out.print(" ");

            }

            for (int k = 1; k <= (2 * i - 1); k++) {

                System.out.print("*");

            }

            System.out.println();

        }

    }

}


3. Diamond Pattern

      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *

Python

def diamondPattern(n):

    for i in range(1, n + 1):

        print(" " * (n - i) + "*" * (2 * i - 1))

    for i in range(n - 1, 0, -1):

        print(" " * (n - i) + "*" * (2 * i - 1))

diamondPattern(7)


Java

public class DiamondPattern {

    public static void main(String[] args) {

        int n = 7;

        int s = n - 1;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= s; j++) {

                System.out.print(" ");

            }

            s--;

            for (int j = 1; j <= 2 * i - 1; j++) {

                System.out.print("*");

            }

            System.out.println();

        }

        s = 1;

        for (int i = 1; i <= n - 1; i++) {

            for (int j = 1; j <= s; j++) {

                System.out.print(" ");

            }

            s++;

            for (int j = 1; j <= 2 * (n - i) - 1; j++) {

                System.out.print("*");

            }

            System.out.println();

        }

    }

}


4. Hollow Rectangle Pattern

* * * * * * * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * * * * * * *
Python

def hollowRectanglePattern(r, c):

    for i in range(r):

        for j in range(c):

            if i == 0 or i == r - 1 or j == 0 or j == c - 1:

                print("* ", end="")

            else:

                print("  ", end="")

        print()

hollowRectanglePattern(7, 14)


Java

public class HollowRectanglePattern {

    public static void main(String[] args) {

        int r = 7;

        int c = 14;

        for (int i = 1; i <= r; i++) {

            for (int j = 1; j <= c; j++) {

                if (i == 1 || i == r || j == 1 || j == c) {

                    System.out.print("* ");

                } else {

                    System.out.print("  ");

                }

            }

            System.out.println();

        }

    }

}


5. Hollow Triangle Pattern 

            * 
          * * 
        *   * 
      *     * 
    *       * 
  *         * 
* * * * * * * 

Python

def hollowTrianglePattern(n):

    for i in range(1, n + 1):

        for j in range(1, n + 1):

            if j == n or i == n or i + j == n + 1:

                print("*", end=" ")

            else:

                print(" ", end=" ")

        print()

hollowTrianglePattern(7)


Java

public class HollowTrianglePattern {

    public static void main(String[] args) {

        int n = 7;

         for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= n; j++) {

                if (j == n || i == n || i + j == n + 1) {

                    System.out.print("* ");

                } else {

                    System.out.print("  ");

                }

            }

            System.out.println();

        }

    }

}


6. Cross Pattern

*           * 
  *       *   
    *   *     
      *       
    *   *     
  *       *   
*           * 

Python

def crossPattern(n):

    for i in range(1, n + 1):

        for j in range(1, n + 1):

            if j == i or j == n - i + 1:

                print("*", end=" ")

            else:

                print(" ", end=" ")

        print()

crossPattern(7)


Java

public class CrossPattern {

    public static void main(String[] args) {

        int n = 7;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= n; j++) {

                if (j == i || j == n - i + 1)

                    System.out.print("* ");

                else

                    System.out.print("  ");

            }

            System.out.println();

        }

    }

}


7. Floyd's Triangle Pattern

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 

Python

def floydsTrianglePattern(n):

    num = 1

    for i in range(1, n + 1):

        for j in range(1, i + 1):

            print(num, end=" ")

            num += 1

        print()

floydsTrianglePattern(7)


Java

public class FloydsTriangle {

    public static void main(String[] args) {

        int n = 7;

        int num = 1;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= i; j++) {

                System.out.print(num + " ");

                num++;

            }

            System.out.println();

        }

    }

}


8. Pascal's Triangle Pattern

                 1
               1   1
             1   2   1
           1   3   3   1
         1   4   6   4   1
       1   5  10  10   5   1
     1   6  15  20  15   6   1

Python

def pascalTrianglePattern(n):

    for i in range(n):

        num = 1

        print("  " * (n - i), end="")

        for j in range(0, i + 1):

            print("{:4}".format(num), end="")

            num = num * (i - j) // (j + 1)

        print()

pascalTrianglePattern(7)


Java

public class PascalTrianglePattern {

    public static void main(String[] args) {

        int n = 7;

        for (int i = 0; i < n; i++) {

            int num = 1;

            System.out.format("%" + (n - i) * 2 + "s", "");

            for (int j = 0; j <= i; j++) {

                System.out.format("%4d", num);

                num = num * (i - j) / (j + 1);

            }

            System.out.println();

        }

    }

}


9. Number Pattern

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 

Python

def numberPattern(n):

    for i in range(1, n + 1):

        for j in range(1, i + 1):

            print(j, end=" ")

        print()

numberPattern(7)


Java

public class NumberPattern {

    public static void main(String[] args) {

        int n = 7;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= i; j++) {

                System.out.print(j + " ");

            }

            System.out.println();

        }

    }

}


10. Alphabet Pattern

A 
B C 
D E F 
G H I J 
K L M N O 
P Q R S T U 

Python

def alphabeticPattern(n):

    ch = 'A'

    for i in range(1, n + 1):

        for j in range(1, i + 1):

            print(ch, end=" ")

            ch = chr(ord(ch) + 1)

        print()

alphabeticPattern(6)


Java

public class AlphabeticPattern {

    public static void main(String[] args) {

        int n = 6;

        char ch = 'A';


        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= i; j++) {

                System.out.print(ch + " ");

                ch++;

            }

            System.out.println();

        }

    }

}


Try exploring more Pattern Programs!

Explore the symmetrical shapes for the above given sample Pattern Programs.

Try using Scanner class and int(input()) to get user input for the number of rows and columns.

Explore more patterns! Wishing to hear from you learners about more pattern ideas in the comments! 

Happy Coding dear Explorers!! :)


Comments

  1. Hi Mam, I think learning about patterns are very fun and interesting because the output is fun to watch with emojis.

    ReplyDelete
    Replies
    1. Coding is a sea! Let's dive into the same and get lots and lots of precious things! Let's explore more! 🥰

      Delete

Post a Comment