Print Pencil Shape Pattern in Java
Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program.
To write the code for it, we take the following approach:
- Take user input and store it in a variable called row.
- To print the pencil, we use a nested loop in which the first loop is responsible for the number of rows and the second loop is responsible for the number of columns.
The pencil pattern is printed in two ways: using the static star character and using the user input character.
Let's look at each one separately to see how we can use static and user-input characters:
Static Star Character
Filename: PencilExample1.java
// Import any necessary classes and packages.
import java.util.Scanner ;
// a class called PencilExample1 should be created to print
//pencil pattern using static star characters.
public class PencilExample1{
// main() method start
public static void main(String[] args){
// declare variables
int rows ;
// make an object of the Scanner class to receive user input.
Scanner sc = new Scanner(System.in) ;
System.out.print("Enter number of rows: ") ;
//user input is taken and stored in the rows variable.
rows = sc.nextInt() ;
//call drawPencil() method of the Pencil class by creating an instance of the class
Pencil b = new Pencil() ;
b.drawPencil(rows) ;
}
}
// create a separate class for Pencil pattern
class Pencil{
// create a user-defined method for drawing pattern
public void drawPencil(int rows){
// declare variables
int t ;
// use for loop from -row to +row
for(int i = -rows; i <= rows; i++)
{
t = i;
// for loop for columns
for(int j = 0; j <= rows; j++)
{
// check whether the value of t is less than j
if(t <= j)
System.out.print("* ") ;
else
System.out.print(" ") ;
}
System.out.println("") ;
}
}
}
Output

User Input Character
Program
Filename: PencilExample2.java
import java.util.Scanner ;
// create class PencilExample2 to print pencil pattern by using user input character
public class PencilExample2{
// main() method start
public static void main(String[] args){
// declare variables
int rows ;
// create a Scanner class object to take input from the user
Scanner sc = new Scanner(System.in) ;
// take input from the user and store it into rows variable
System.out.print("Enter number of rows: ") ;
rows = sc.nextInt() ;
System.out.print("Enter any character : ") ;
char c = sc.next().charAt(0) ;
//call drawPencil() method of the Pencil class by creating an instance of the class
Pencil b = new Pencil() ;
b.drawPencil(rows, c) ;
}
}
// create a separate class for Pencil pattern
class Pencil{
// create a user-defined method for drawing pattern
public void drawPencil(int rows, char c){
// declare variables
int t ;
// use for loop from -row to +row
for(int i = -rows; i <= rows; i++)
{
t = i ;
// for loop for columns
for(int j = 0; j <= rows; j++)
{
// check whether the value of t is less than j
if(t <= j)
System.out.print(c+" ") ;
else
System.out.print(" ") ;
}
System.out.println("") ;
}
}
}
Output 1

Explanation
Let's examine how the for loop in the code above that prints the pencil pattern functions in each iteration.
Enter number of rows: 2
Enter any character: *
Iteration-I
- The first iteration will begin at i = -2 and continue until i <= 2.
- Let's say the t variable will be used to record the value of i, which is -2.
- From j = 0 to j <= row, the second for loop will loop through the data.
- The condition t <= j determines the star that appears on the screen. As a result, this condition will be met three times in the first iteration of the main for loop, printing three stars on the screen.
Iteration-II
- The first iteration will begin with i = -1 and continue until i <= 2.
- A variable, let's say t, will be used to record the value of i, which is -1.
- From j = 0 to j <= row, the second for loop will loop through the data.
- The three stars will appear on the screen if condition t <= j is met three times.
Iteration-III
- The first iteration will begin with i = 0 and run until i< = 2.
- We'll save the value of i, which is 0, in a variable called t.
- From j = 0 to j <= row, the second for loop will loop through the data.
- If the condition t <= j is met three times, the screen will display three stars; if not, it will display a space.
Iteration-IV
- The first iteration will begin with i = 1 and run until i <= 2.
- A variable called t will be used to store the value of i, which is 1.
- From j = 0 to j <= row, the second for loop will loop through the data.
- If the condition t <= j is met twice, two stars will be printed on the screen; else, space will be printed.
Iteration-V
- The first iteration will begin at i = 2 and continue until i <= 2.
- A variable called t will be used to record the value of i, which is 2.
- From j = 0 to j <= row, the second for loop will loop through the data.
- One star will be printed on the screen if condition t <= j is satisfied; otherwise, space will be printed.
The variable i will be 3 after the fifth iteration, and the first for loop condition will be false. The pencil pattern will finally appear on the console.
Output 2

Explanation
Let's examine how the for loop in the code above that prints the pencil pattern functions in each iteration.
Enter number of rows: 2
Enter any character: #
Iteration-I
- The first iteration will begin at i = -2 and continue until i <= 2.
- Let's say the t variable will be used to record the value of i, which is -2.
- From j = 0 to j <= row, the second for loop will loop through the data.
- The condition t <= j determines the hash that appears on the screen. As a result, this condition will be met three times in the first iteration of the main for loop, printing three hash on the screen.
Iteration-II
- The first iteration will begin with i = -1 and continue until i <= 2.
- A variable, let's say t, will be used to record the value of i, which is -1.
- From j = 0 to j <= row, the second for loop will loop through the data.
- The three hash will appear on the screen if condition t <= j is met three times.
Iteration-III
- The first iteration will begin with i = 0 and run until i< = 2.
- We'll save the value of i, which is 0, in a variable called t.
- From j = 0 to j <= row, the second for loop will loop through the data.
- If the condition t <= j is met three times, the screen will display three hash; if not, it will display a space.
Iteration-IV
- The first iteration will begin with i = 1 and run until i <= 2.
- A variable called t will be used to store the value of i, which is 1.
- From j = 0 to j <= row, the second for loop will loop through the data.
- If the condition t <= j is met twice, two hashes will be printed on the screen; else, space will be printed.
Iteration-V
- The first iteration will begin at i = 2 and continue until i <= 2.
- A variable called t will be used to record the value of i, which is 2.
- From j = 0 to j <= row, the second for loop will loop through the data.
- One hash will be printed on the screen if condition t <= j is satisfied; otherwise, space will be printed.
The variable i will be 3 after the fifth iteration, and the first for loop condition will be false. The pencil pattern will finally appear on the console.
Pencil Pattern Program in Java
Filename: Main.java
import java.util.Scanner;
public class Main{
public static void main(String[] args){
int r,c,t,row ;
Scanner sc= new Scanner(System.in) ;
System.out.print("Enter the Value for row : ") ;
row=sc.nextInt() ;
// this loop will execute from -row to +row
for(r=-row; r<=row; r++)
{
t=r ;
for(c=0; c<=row; c++)
{
if(t<=c)
System.out.print(c+" ") ;
else
System.out.print(" ") ;
}
System.out.println("") ;
}
}
}
Output
