Java Program to Print Pencil Shape Star Pattern

Program to Print Pencil Shape Star Pattern

In this article we are going to see how to print the Pencil star pattern

Example-1

If the user input is 2
* * *
* * *
* * *
 * *
  *
Example-2

If the user input is 4
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
 * * * *
  * * *
   * *
    *

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Approach:

  • Enter total row and store it an integer variable say row.
  • Take first for loop to print all the rows.
  • Take inner loop to print the column values.

JAVA Code:

Methdo-1 : Static Star Character

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
    int r,c,h,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++) 
    {
        h=r;
 
        for(c=0; c<=row; c++)
        {
            if(h<=c)
                System.out.print("* ");
            else
                System.out.print(" ");
        }
         System.out.println("");
    }
    }
}

Output:

Enter the Value for row: 2
*  *  *
*  *  *
*  *  *
 *  *
   *

Method-2 : User Input Character

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
    int r,c,h,row;
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter the Value for row : ");
    row=sc.nextInt();
    
    System.out.print("Enter any character : ");
    char s=sc.next().charAt(0);
    
    // this loop will execute from -row to +row
    for(r=-row; r<=row; r++) 
    {
        h=r;
 
        for(c=0; c<=row; c++)
        {
            if(h<=c)
                System.out.print(s+" ");
            else
                System.out.print(" ");
        }
         System.out.println("");
    }
    }
}


Output:

Enter the value for row :
2
Enter any character : @
@ @ @
@ @ @
@ @ @
  @ @
    @

Explanation:

Let’s understand the program with detailed explanation.

Let we have taken row as 2.

Iteration-I

r=-2 (passed through first for loop condition) which will execute till r<=2.

The r value will be store in a variable say h.

Then inner for loop will iterate from c=0 till c<=row.

If h<=c print the symbol 3 times, otherwise print space.

***

Iteration-II

r=-1 (passed through first for loop condition) which will execute till r<=2.

The r value will be store in a variable say h.

Then inner for loop will iterate from c=0 till c<=row.

If h<=c print the symbol 3 times, otherwise print space.

***

Iteration-III

r=0 (passed through first for loop condition) which will execute till r<=2.

The r value will be store in a variable say h.

Then inner for loop it from c=0 till c<=row.

If h<=c print the symbol 3 times, otherwise print space.

***

Iteration-IV

r=1 (passed through first for loop condition) which will execute till r<=2.

The r value will be store in a variable say h. Then inner for loop it from c=0 till c<=row.

If h<=c print the symbol 2 times, otherwise print space.

 * *

Iteration-V

r=2 (passed through first for loop condition) which will execute till r<=2.

The r value will be store in a variable say h.

Then inner for loop it from c=0 till c<=row. If h<=c print the symbol 1 time, otherwise print space.

  *

Now r=3, so first for loop condition fails. And no more for loop will be executed. At last we see a pattern like this.

* * *
* * *
* * *
 * *
  *

C Code:

#include <stdio.h>

int main() {
    int r,c,h,row;
    printf("Enter the Value for h: ");
    scanf("%d",&row);
    for(r=-row; r<=row; r++) 
    {
        h=r;
 
        for(c=0; c<=row; c++)
        {
            if(h<=c)
                printf("* ");
            else
                printf(" ");
        }
         printf("\n");
    }
}

Output:

Enter the Value for h: 2
* * *
* * *
* * *
 * *
  *

C++ Code:

#include <iostream>
using namespace std;

int main() {
    int r,c,h,row;
    cout<<("Enter the Value for h: ");
    cin>>row;
    for(r=-row; r<=row; r++) 
    {
        h=r;
 
        for(c=0; c<=row; c++)
        {
            if(h<=c)
                cout<<("* ");
            else
                cout<<(" ");
        }
         cout<<("\n");
    }
}

Output:

Enter the Value for h: 2
* * *
* * *
* * *
 * *
  *

Related Java Star Pattern Programs: