Home » Print Pencil Shape Pattern in Java

Print Pencil Shape Pattern in Java

by Online Tutorials Library

Print Pencil Shape Pattern in Java

The pencil pattern is another pattern created from asterisk symbols using loop and other logical concepts. It is usually asked to draw pattern through a program.

We use the following approach to write the code for it:

  1. Take input from the user and store it into a variable, say row.
  2. We use a nested loop to print the pencil where the first loop is responsible for the number of rows and the second loop is responsible for the number of columns.

We print the pencil pattern in two ways, i.e., by using the static star character and by using the user input character.

Let’s understand both of them one by one to understand how we can use static and user input characters:

Static Star Character

PencilPatternExample1.java

Output:

Print Pencil Shape Pattern in Java

User Input Character

PencilPatternExample2.java

Output:

Print Pencil Shape Pattern in Java

Explanation:

Let’s understand how each iteration of the for loop works in the above code to print the pencil pattern.

Enter number of rows: 2

Enter any character: @

Iteration-I

  1. The 1st iteration will be start from i = -2 and execute till i <= 2.
  2. The value of i, i.e., -2, will be stored in a variable, say h.
  3. Th second for loop will iterate from j = 0 till j <= row.
  4. Condition h <= j determines the star to print on the screen. So, In the first iteration of the main for loop, this condition will be satisfied three times and print three-star on the screen.

Iteration-II

  1. The 1st iteration will be start from i = -1 and execute till i <= 2.
  2. The value of i, i.e., -1, will be stored in a variable, say h.
  3. Th second for loop will iterate from j = 0 till j <= row.
  4. Condition h <= j will be satisfied three times and print three-star on the screen.

Iteration-III

  1. The 1st iteration will be start from i = 0 and execute till i <= 2.
  2. The value of i, i.e., 0, will be stored in a variable, say h.
  3. Th second for loop will iterate from j = 0 till j <= row.
  4. Condition h <= j will be satisfied three times and print three-star on the screen else print space.

Iteration-IV

  1. The 1st iteration will be start from i = 1 and execute till i <= 2.
  2. The value of i, i.e., 1, will be stored in a variable, say h.
  3. Th second for loop will iterate from j = 0 till j <= row.
  4. Condition h <= j will be satisfied two times and print two stars on the screen else print space.

Iteration-V

  1. The 1st iteration will be start from i = 2 and execute till i <= 2.
  2. The value of i, i.e., 2, will be stored in a variable, say h.
  3. Th second for loop will iterate from j = 0 till j <= row.
  4. Condition h <= j will be satisfied only one time and print one star on the screen else print space.

After the 5th iteration, i will be 3, and the first for loop condition will be false. At last, the pencil pattern will print on the console.


You may also like