(Solution) Must Do Pattern Problems before starting DSA - Striver DSA Course
Tags: dsa, java, programming
Published: 2022-08-22 14:44:00

In this post, I have given the solutions to the pattern worksheet of DSA course by Striver. Codes are written in JAVA but to get c++ code just copy the code inside main function and replace the print statement of java with that of C++.

I hope you will like it. Do let me know if you want more such solutions.

---

- ![Pattern 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/930y19qd49en1a96lbxf.png)
```
public class Program
{
    public static void main(String[] args) {
		int count = 5;
        for(int i = 0; i<count; i++){
            for(int j = 0; j<count; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rsry9dqlg162uk8q99l.png)
 
- ![Pattern 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5yx98m3fwb8rw7avrmw.png)
```
public class Program
{
    public static void main(String[] args) {
		int count = 5;
        for(int i = 0; i<count; i++){
            for(int j = 0; j<=i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfa5yertk9ttv411lwa3.png)
 
 
- ![Pattern 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/legcirh5q2si9fhavzs1.png)
```
public class Program
{
    public static void main(String[] args) {
		int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j<=i; j++){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rdyvpopbny9euaa9zdu.png)

- ![Pattern 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywha31lbogsxtvmjhprh.png)
 
```
public class Program
{
    public static void main(String[] args) {
		int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j<=i; j++){
                System.out.print(i);
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2gif3y4m3b8hi3elm80m.png)
 
- ![Pattern 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/li744uhv2gebxz1wy9k5.png)
  
```
public class Program
{
    public static void main(String[] args) {
		int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = count; j >= i; j--){
                System.out.print("*");
            }
            System.out.println();
        }
	}
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ntv17m2xlekb66f2ybo.png)
 
- ![Pattern 6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/solux5sqxqb97vgzzm33.png)
 
```
public class Program
{
    public static void main(String[] args) {
		int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = count; j >= i; j--){
                System.out.print(count-j+1);
            }
            System.out.println();
        }
	}
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wpiv03ffcznk6o160hsy.png)
 
- ![Pattern 7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76wg03w0ot0w50tpj0jy.png)
 
```
public class Program
{
    public static void main(String[] args) {
		int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = count; j > i; j--){
                System.out.print(" ");
            }
            for(int j = 1; j <= 2*i-1; j++){
                System.out.print("*");
            }
            System.out.println();
        }
	}
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q21hv9l3d1bk3rqyr03h.png)
 
- ![Pattern 8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzcwulz7oxlyfs8yfiyh.png)

```
public class Program
{
    public static void main(String[] args) {
		int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j < i; j++){
                System.out.print(" ");
            }
            for(int j = 2*(count - i)+1; j >0; j--){
                System.out.print("*");
            }
            System.out.println();
        }
	}
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfgijtbfhhd0x6280wcz.png)
 
 
- ![Pattern 9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6nt4jacpn05nrzxah0v.png)
 
```
public class Program
{
    public static void main(String[] args) {
		int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = count; j > i; j--){
                System.out.print(" ");
            }
            for(int j = 1; j <= 2*i-1; j++){
                System.out.print("*");
            }
            System.out.println();
        }

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

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0x4wx369j3bb2zwparm5.png)
 
- ![Pattern 10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frbz0ttdszk208j5ybba.png)

```
public class Program
{
    public static void main(String[] args) {
		int count = 5;
        for(int i = 0; i<count; i++){
            for(int j = 0; j<=i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
        for(int i = 1; i<=count; i++){
            for(int j = count; j >= i; j--){
                System.out.print("*");
            }
            System.out.println();
        }     
	}
}
``` 

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exuyvy3lp922e9b1jxbk.png)
 

- ![Pattern 11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugkfgk5on1gxj9a1z6u3.png)
 
```
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        int one = 1;
        for(int i = 0; i<count; i++){
            if(i%2!=0) one = 0;
            else one = 1;
            for(int j = 0; j<=i; j++){
                System.out.print(one + " ");
                if(one==1) one = 0;
                else one = 1;
            }
            System.out.println();
        }

    }
}

```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1z3dqur3ta1cudak6y0.png)
 
 
- ![Pattern 12](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vt9l87bdbynbavkho02.png)
 
```
public class Program
{
    public static void main(String[] args) {
		int count = 4;
        for(int i = 1; i<=count; i++){
            for(int j=1; j<=i;j++){
                System.out.print(j);
            }
            for(int j=1; j<=2*(count-i);j++){
                System.out.print(" ");
            }
            for(int j=i; j>0;j--){
                System.out.print(j);
            }
            System.out.println();
        }
        
	}
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yaq22oa5ptsr6fe0cki0.png)
 
- ![Pattern 13](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qssxd3xedl9tsppvtxn0.png)
 
```
public class Program
{
    public static void main(String[] args) {
        int count = 5; int num=1;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j<=i; j++){
                System.out.print(num++ + " ");
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzerelrlgebxc0b50nyc.png)
 
- ![Pattern 14](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8b3jcjptq5sh2or5tw1.png)
 
```
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            char alp = 'A';
            for(int j = 1; j<=i; j++){
                System.out.print(alp++);
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuqhaw3b3w72wufdu737.png)
 
- ![Pattern 15](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tzj4r3ag2hwyj9p8k9nw.png)

```
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            char alp = 'A';
            for(int j = count; j >= i; j--){
                System.out.print(alp++);
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arjjgxk9sc44a292qgha.png)
 
- ![Pattern 16](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p6oeo9xctnhlep0f27d.png)
 
```
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        char alp = 'A';
        for(int i = 1; i<=count; i++){
            for(int j = 1 ; j <= i; j++){
                System.out.print(alp);
            }
            System.out.println();
            alp++;
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtexqwdd2nge0igq5r7q.png)
 
 
- ![Pattern 17](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4at8bttez68u5a0vwt8.png)
 
```
public class Program
{
    public static void main(String[] args) {
        int count = 4;
        for(int i = 1; i<=count; i++){
            char alp = 'A';
            for(int j = count ; j > i; j--){
                System.out.print(" ");
            }
            for(int j = 1 ; j <= i; j++){
                System.out.print(alp++);
            }
            --alp;
            for(int j = 1 ; j < i; j++){
                System.out.print(--alp);
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uqq1xx5eli6seh46du49.png)
 
- ![Pattern 18](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7mrc460tmc1cjxnt16ho.png)
 
```
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            int alp = 65 + count - 1;
            for(int j = i-1 ; j >= 0; j--){
                int result = alp-j;
                System.out.print( (char) result);
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c76jhdkljqyyqgst3y3l.png)
 
- ![Pattern 19](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29oml0265x5m818gtk5n.png)
 
```
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = count; j >= i; j--){
                System.out.print("*");
            }
            for(int j = 1; j < 2*i-1; j++){
                System.out.print(" ");
            }
            for(int j = count; j >= i; j--){
                System.out.print("*");
            }
            System.out.println();
        }
         for(int i = 1; i<=count; i++){
            for(int j = 1; j <= i; j++){
                System.out.print("*");
            }
            for(int j = 2*(count - i)-1; j >=0; j--){
                System.out.print(" ");
            }
            for(int j = 1; j <= i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fuikbx8pddrptvsomlrp.png)
 
- ![Pattern 20](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nrdenk2omjxuaybkn2pc.png)
 
```
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j <= i; j++){
                System.out.print("*");
            }
            for(int j = 2*(count - i); j>0; j--){
                System.out.print(" ");
            }
            for(int j = 1; j <= i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
        for(int i = 1; i<count; i++){
            for(int j = count; j > i; j--){
                System.out.print("*");
            }
            for(int j = 1; j <= 2*i; j++){
                System.out.print(" ");
            }
            for(int j = count; j > i; j--){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rn3tc01gbnagr9n9af7g.png)
 
- ![Pattern 21](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tym0yrft33556vh59v3g.png)
 
```
public class Program
{
    public static void main(String[] args) {
        int count = 4;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j<=count; j++){
                if(i==1 || i==count || j==1 || j==count){
                    System.out.print("*");
                } else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q98blxzts5muidz4neox.png)

- ![Pattern 22](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9py20ujj8xzd6uwih7t8.png)
 
 
```
public class Program
{
    public static void main(String[] args) {
        int count = 4;
        for(int i = 1; i<=count; i++){
            for(int j=count; j>count-i;j--){
                System.out.print(j+" ");
            }
            for(int j=1; j<=2*(count-i)-1;j++){
                System.out.print(count-i+1+" ");
            }
            for(int j=count-i+1; j<=count;j++){
                if(j==1) continue;
                System.out.print(j+" ");
            }
            System.out.println();
        }
        for(int i = count-1; i>0; i--){
            for(int j=count; j>count-i;j--){
                System.out.print(j+" ");
            }
            for(int j=1; j<=2*(count-i)-1;j++){
                System.out.print(count-i+1+" ");
            }
            for(int j=count-i+1; j<=count;j++){
                if(j==1) continue;
                System.out.print(j+" ");
            }
            System.out.println();
        }
    }
}
```

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u17re6k0ilzyy42a61zx.png)
 

---
The sheet is completed. Share your reactions with me.

Signing off,
This is VAIB