Java Program to Print Arrow Star Pattern

Program to Print Arrow Star Pattern

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

Example-1
When size value=6
                        *
                    **
                ***
            ****
        *****
    ******
*******
    ******
        *****
            ****
                ***
                    **
                       *
Example-2

When size value=5
                    *
                **
            ***
        ****
    *****
******
    *****
        ****
            ***
                **
                   *

Now, let’s see the actual program to print it.

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Approach:

  • Enter size and store it in an integer variable size.
  • Take first for loop to print all rows.
  • Take second/inner for loop to print column values.
  • Then go on printing the star symbols according to the iteration.

JAVA Code:

Method-1 : Static Star Character

import java.util.Scanner;
class Main
{
    public static void main(String[] args)  
    {  
        Scanner scan = new Scanner(System.in);
        System.out.print("Size : ");
        //Taking size as input from user
        int r, c,k, size=scan.nextInt();

    for (r = -size; r <= size; r++)
    {//Outer for loop
        k = r;
        if (k < 0)
        {//To calculate the number of stars printed
            k *= -1;
        }
        for (c = 0; c <= size; c++)
        {//Inner Loop to print star and space
            if (c < k)
                System.out.print("  ");
            else
                System.out.print("*");
        }
        System.out.println();
        
    }
}
}

Output:

Size : 5
                    *
                **
            ***
        ****
    *****
******
    *****
        ****
            ***
                **
                   *

Method-2 : User Input Character

import java.util.Scanner;
class Main
{
    public static void main(String[] args)  
    {  
        int r, c,k,size;
        char ch;
        Scanner scan = new Scanner(System.in);
        System.out.print("Size : ");
        //Taking size as input from use
        size=scan.nextInt();
        System.out.print("Character : ");
        //Taking size as input from use
        ch=scan.next().charAt(0);

    for (r = -size; r <= size; r++)
    {//Outer for loop
        k = r;
        if (k < 0)
        {//To calculate the number of stars printed
            k *= -1;
        }
        for (c = 0; c <= size; c++)
        {//Inner Loop to print star and space
            if (c < k)
                System.out.print("  ");
            else
                System.out.print(ch);
        }
        System.out.println();
        
    }
}
}

Output:

Size : 5
Character : <
                   <
               <<
           <<<
       <<<<
   <<<<<
<<<<<<
   <<<<<
       <<<<
           <<<
               <<
                   <

C Code:

#include <stdio.h>
int main(int argc, char const *argv[])
{
    printf("Size : ");
    //Taking size as input from user
    int size, r, c, k;
    scanf("%d", &size);

    for (r = -size; r <= size; r++)
    {//inner loop
        k = r;
        if (k < 0)
        { //To calculate the number of stars printed
            k *= -1;
        }
        for (c = 0; c <= size; c++)
        { //Inner Loop to print star and space
            if (c < k)
                printf("  ");
            else
                printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output:

Size : 5
                   *
               **
           ***
       ****
   *****
******
   *****
       ****
           ***
               **
                   *

C++ Code:

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    cout << "Size : ";
    //Taking size as input from user
    int size, r, c, k;
    cin >> size;

    for (r = -size; r <= size; r++)
    { //inner loop
        k = r;
        if (k < 0)
        { //To calculate the number of stars printed
            k *= -1;
        }
        for (c = 0; c <= size; c++)
        { //Inner Loop to print star and space
            if (c < k)
                cout << "  ";
            else
                cout << "*";
        }
        //Prints a new line
        cout << endl;
    }
    return 0;
}
Output:

Size : 5
                   *
               **
           ***
       ****
   *****
******
   *****
       ****
           ***
               **
                   *

Related Java Star Pattern Programs:

Leave a Comment