Java Program to Print Inverted V Star Pattern

Program to Print Inverted V Star Pattern

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

Example-1

When rows value = 5
    *     
   * *    
  *   *   
 *     *  
*       *
Example-2

When rows value = 6
     *     
    * *    
   *   *   
  *     *  
 *       * 
*         *

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

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Approach:

  • Enter the number of rows to print and store it in an integer variable rows.
  • Take first for loop to print all rows.
  • Take inner for loop to print column values and one to print empty spaces.
  • 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)  
    {  
        int r,s=0, c, rows;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Rows : ");
        //Taking total rows as input from user
        rows = scan.nextInt();

        for(r = rows-1; r>= 0; r--)
        {//Outer Loop
            for(c = rows-1;c>s;c--)
            //Inner loop to print first half star
                System.out.print(" ");
            System.out.print("*");
            for(c=1;c<(s*2);c++)
            //Inner loop to print mid gap
                System.out.print(" ");
            if(r<rows-1)
            //Condition to print second half star
                System.out.print("*");
            s++;//counter
            //Prints a newline
            System.out.println();
        }
    }
}

Output

Enter Rows : 9
        *
       * *
      *   *
     *     *
    *       *
   *         *
  *           *
 *             *
*               *

Method-2 : User Input Character

import java.util.Scanner;
class Main
{
    public static void main(String[] args)  
    {  
        int r,s=0, c, rows;
        char d;
        
        Scanner scan = new Scanner(System.in);
        
         //Taking total rows as input from user
        System.out.print("Enter Rows : ");
        rows = scan.nextInt();
        
         //Taking any random character as input from user
        System.out.print("Enter Character : ");
        d = scan.next().charAt(0);

        for(r = rows-1; r>= 0; r--)
        {//Outer Loop
            for(c = rows-1;c>s;c--)
            //Inner loop to print first half star
            System.out.print(" ");
            System.out.print(d);
            for(c=1;c<(s*2);c++)
            //Inner loop to print mid gap
                System.out.print(" ");
            if(r<rows-1)
            //Condition to print second half star
                System.out.print(d);
            s++;//counter
            //Prints a newline
            System.out.println();
        }
    }
}

Output

Enter Rows : 9
Enter Character : v
        v
       v v
      v   v
     v     v
    v       v
   v         v
  v           v
 v             v
v               v

C Code:

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

    for (r = rows - 1; r >= 0; r--)
    { //Outer Loop
        for (c = rows - 1; c > s; c--)
            //Inner loop to print first half star
            printf(" ");
        printf("*");
        for (c = 1; c < (s * 2); c++)
            //Inner loop to print mid gap
            printf(" ");
        if (r < rows - 1)
            //Condition to print second half star
            printf("*");
        s++; //counter
        //Prints a new line
        printf("\n");
    }
    return 0;
}
Output

Enter Rows : 9
        *
       * *
      *   *
     *     *
    *       *
   *         *
  *           *
 *             *
*               *

C++ Code:

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

    for (r = rows - 1; r >= 0; r--)
    { //Outer Loop
        for (c = rows - 1; c > s; c--)
            //Inner loop to print first half star
            cout << " ";
        cout << "*";
        for (c = 1; c < (s * 2); c++)
            //Inner loop to print mid gap
            cout << " ";
        if (r < rows - 1)
            //Condition to print second half star
            cout << "*";
        s++; //counter
        //Prints a new linee
        cout << endl;
    }
    return 0;
}

Output

Enter Rows : 9
        *
       * *
      *   *
     *     *
    *       *
   *         *
  *           *
 *             *
*               *

Related Java Star Pattern Programs: