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.
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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++;
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
The sheet is completed. Share your reactions with me.
Signing off,
This is VAIB
Top comments (1)
Great work. Will be helpful for many people!
Keep sharing.