Search Logic Blocks

Wednesday, February 12, 2020

Java: Let's Practice - Create different patterns with numbers - Diamond

Write a program to create a pattern like below - 

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
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9

Step1: Count the number of rows and columns needed.

Total no of columns - 9 and total no of rows - 9

Step2: Make a grid to see what character will fall in which row and which column. And notice if there is some pattern (repeating characters in some particular way).


1 2 3 4 5 6 7 8 9
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 1 2 3 4 5 6 7
8 1 2 3 4 5 6 7 8
9 1 2 3 4 5 6 7 8 9

Here, we notice that numbers are incremented in every row and it's a diagonal. So, here we can write one print statement for each row or we can have for loop to print the numbers diagonally. There might be other methods too, but I will show you only two methods.

Step3: Start writing a Java program by crating a class and main method.

public class CreatePattern {
    public static void main(String args[]) {
        
    }
}
Step4: In main method - 
Method1: write sout (System.out.println) statements equal to number of rows required. So, there are 9 rows means 9 println statements.
System.out.println("1");
System.out.println("1 2");
System.out.println("1 2 3");
System.out.println("1 2 3 4");
System.out.println("1 2 3 4 5");
System.out.println("1 2 3 4 5 6");
System.out.println("1 2 3 4 5 6 7");
System.out.println("1 2 3 4 5 6 7 8");
System.out.println("1 2 3 4 5 6 7 8 9");
Method2: write for loop - as you can see that there is a pattern. 1st row has only number 1, 2nd row has 2 numbers 1,2 and so on.... We need here nested for loops (one for loop in other for loop) - first for loop is for each row and inner loop is for each column.

for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= i; j++) {
        System.out.print(j + " ");
    }
    System.out.println("");
}
Here, notice that I am using print statement in the for loop to write the numbers in one row and after end of inner loop, I have a println statement to create next row. Outer loop is run for 9 times for 9 rows. Inner loop runs only i times (1st row - 1, 2nd row - 2 times, and so on....) and j is printed as j value starts from 1 and is increnmented till i.

Step5: Here is the final program with both methods -
Method1: 

Class CreatePattern1 (CreatePattern1.java)
public class CreatePattern1 {
    public static void main(String args[]) {
        System.out.println("1");
        System.out.println("1 2");
        System.out.println("1 2 3");
        System.out.println("1 2 3 4");
        System.out.println("1 2 3 4 5");
        System.out.println("1 2 3 4 5 6");
        System.out.println("1 2 3 4 5 6 7");
        System.out.println("1 2 3 4 5 6 7 8");
        System.out.println("1 2 3 4 5 6 7 8 9"); 
    }
}
Method2: 

Class CreatePattern2 (CreatePattern2.java)
public class CreatePattern2 {
    public static void main(String args[]) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println("");
        }
    }
}
Let's try another pattern (Diamond) -

            1
         2 1 2
      3 2 1 2 3
   4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
   4 3 2 1 2 3 4
      3 2 1 2 3
         2 1 2
            1

Step1: Let's count the number of rows and columns needed -

Total no of 9 columns and 9 rows required.

Step2: Make a grid of pattern -


1 2 3 4 5 6 7 8 9
1 1
2 2 1 2
3 3 2 1 2 3
4 4 3 2 1 2 3 4
5 5 4 3 2 1 2 3 4 5
6 4 3 2 1 2 3 4
7 3 2 1 2 3
8 2 1 2
9 1

Here, we notice that numbers form diamond, numbers are increasing in both directions and then decreasing in both directions. We need a lot of for loops going up and down.

Step3: Start writing the program by creating the class and main method.
public class CreateDiamondPattern {
    public static void main(String args[]) {
        
    }
}
Step4: In the main method - we require different loops for increasing numbers and decreasing numbers. Let's start with number of rows - for loop for 9 rows -
for (int i = 1; i <= 9; i++) {
    
}
This is the main outer loop which will run for 9 times for value of i from 1 to 9. As you notice that the numbers are increasing till row 5 and then the numbers are decreasing. So, we will create two separate inner for loops for rows 1 to 5 and 6 to 9. Here, we write an if statement to check row number, if it is less than or equal to 5 or not. If part and else parts will have separate for loops -
for (int i = 1; i <= 9; i++) {
    if(i <= 5) {
        
    } else {
        
    }
    System.out.println("");
}
Here, the in the outer loop, we have a condition for each row. First 5 rows there is a diffrferent pattern and remaining 4 rows there is a different pattern. When row is completed println statement goes to next row. 

First 5 rows (if part)
decreasing the number of spaces in first 4 columns - 
for (int j = i; j < 5; j++) {
    System.out.print("  ");
}
increasing number in each row but counting backwards for first 5 columns -
for (int j = i; j >= 1; j--) {
    System.out.print(j + " ");
}
increasing number in each row and counting forward for remaining 4 columns -
for (int j = 2; j <= i; j++) {
    System.out.print(j + " ");
}
so, integrated code for first rows is -
if(i <= 5) {
    for (int j = i; j < 5; j++) {
        System.out.print("  ");
    }
    for (int j = i; j >= 1; j--) {
        System.out.print(j + " ");
    }
    for (int j = 2; j <= i; j++) {
        System.out.print(j + " ");
    }
}
Last 4 rows (else part)
increasing the number of spaces in first 4 columns -
for (int j = i; j > 5; j--) {
    System.out.print("  ");
}
decreasing number in each row (6 to 9) but counting backwards for first 5 columns -
for (int j = (10-i); j >=1; j--) {
    System.out.print(j + " ");
}
decreasing number in each row and counting forward for remaining 4 columns -
for (int j = 2; j <= (10-i); j++) {
    System.out.print(j + " ");
}
so, integrated code for last 4 rows is -
else {
    for (int j = i; j > 5; j--) {
        System.out.print("  ");
    }
    for (int j = (10-i); j >=1; j--) {
        System.out.print(j + " ");
    }
    for (int j = 2; j <= (10-i); j++) {
        System.out.print(j + " ");
    }

}
Step5: Here is the full code to create the diamond pattern -

Class CreateDiamondPattern (CreateDiamondPattern.java)
public class CreateDiamondPattern {
    public static void main(String args[]) {
        for (int i = 1; i <= 9; i++) {
            if(i <= 5) {
                for (int j = i; j < 5; j++) {
                    System.out.print("  ");
                }
                for (int j = i; j >= 1; j--) {
                    System.out.print(j + " ");
                }
                for (int j = 2; j <= i; j++) {
                    System.out.print(j + " ");
                }
            } else {
                for (int j = i; j > 5; j--) {
                    System.out.print("  ");
                }
                for (int j = (10-i); j >=1; j--) {
                    System.out.print(j + " ");
                }
                for (int j = 2; j <= (10-i); j++) {
                    System.out.print(j + " ");
                }

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

No comments: