Java Program to Print Alphabet Q Star Pattern

Program to Print Alphabet Q Star Pattern

In this article we are going to see how to print Q  star program.

Example-1

When row value=5      
 ****
*      *
*      *
*      *
 ****
      *
       *
Example-2:

When row value=6
   ***** 
 *        *
*          *
*          *
*          *
 *******
          *
           *
            *

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

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

Approach:

  • Enter total row and store it in an integer variable row.
  • Take variable s  for space and w for width .
  • Calculate space and width .
  • Take first for loop to print for  (“o”) the row value  .
      • Take first inner for loop to print column value i.e., star  according to condition  if (c == w  -  abs(s) || c ==  abs(s)) and
        if ((r == 0 || r == row - 1) && c >  abs(s) && c < w  -  abs(s))  else it will print space .
      • For each condition if (s  != 0 && r < row  / 2) and if (r >= (row  / 2 + row  / 5)) decreases the space value .
  • Take counter value d = row ;
  • Take second outer for loop to print last part of Q .
      • Take a first inner for loop to print column value for printing star  for the condition if (c==d) else print space .
      • For each iteration increase the d value .
  • Then go on printing the star symbol according to loop.

JAVA Code:

Method-1: Static Star Character

import java.util.*;
import java.lang.Math;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,s,w ;
    //creating object 
    Scanner sc = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = sc.nextInt();
    //outer for loop 
    s  = (row  / 3);
    w  = row  / 2 + row  / 5 + s  + s ;
    //Printing o part 
    for (r = 0; r < row ; r++) 
    {
        for (c = 0; c <= w ; c++) 
        {
            if (c == w  - Math.abs(s) || c == Math.abs(s))
                System.out.print("*");
            else if ((r == 0 || r == row - 1) && c > Math.abs(s) && c < w  - Math.abs(s))
                System.out.print("*");
            else
                System.out.print(" ");
        }
        if (s  != 0 && r < row  / 2) 
            s --;
        else if (r >= (row  / 2 + row  / 5))
            s --;
        System.out.print("\n");
    }
    int  d=row; 
    for (r = 0; r < row  / 2; r++)
    {
        for (c = 0; c <= d; c++) 
        {
            if (c == d)
                System.out.print("*");
            else
                System.out.print(" ");
        }
        System.out.print("\n");
        d++;
    }
    }
}
Output :

Enter row :  5 

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

Method-2: User Input Character

import java.util.*;
import java.lang.Math;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,s,w ;
    char q;
    //creating object 
    Scanner sc = new Scanner(System.in);
    
    // entering the number of row
    System.out.print("Enter rows : ");
    row = sc.nextInt();
    
    // entering any random character
    System.out.print("Enter character : ");
    q = sc.next().charAt(0);
    
    //outer for loop 
    s  = (row  / 3);
    w  = row  / 2 + row  / 5 + s  + s ;
    //Printing o part 
    for (r = 0; r < row ; r++) 
    {
        for (c = 0; c <= w ; c++) 
        {
            if (c == w  - Math.abs(s) || c == Math.abs(s))
                System.out.print(q);
            else if ((r == 0 || r == row - 1) && c > Math.abs(s) && c < w  - Math.abs(s))
                System.out.print(q);
            else
                System.out.print(" ");
        }
        if (s  != 0 && r < row  / 2) 
            s --;
        else if (r >= (row  / 2 + row  / 5))
            s --;
        System.out.print("\n");
    }
    int  d=row; 
    for (r = 0; r < row  / 2; r++)
    {
        for (c = 0; c <= d; c++) 
        {
            if (c == d)
                System.out.print(q);
            else
                System.out.print(" ");
        }
        System.out.print("\n");
        d++;
    }
    }
}

Output :

Enter row :  5 
Enter character: g
  gggg
g        g
g        g
g        g
  gggg
         g
           g

C Code:

#include <stdio.h>
#include <stdlib.h>
int main() {
   int r, row, c ,d,s,w;
   printf("Enter rows: ");
   scanf("%d", &row);
    s  = (row  / 3);
    w  = row  / 2 + row  / 5 + s  + s ;
    for (r = 0; r < row ; r++) 
    {
        for (c = 0; c <= w ; c++) 
        {
            if (c == w  -  abs(s) || c ==  abs(s))
                 printf("*");
            else if ((r == 0 || r == row - 1) && c >  abs(s) && c < w  -  abs(s))
                 printf("*");
            else
                 printf(" ");
        }
        if (s  != 0 && r < row  / 2) 
            s --;
        else if (r >= (row  / 2 + row  / 5))
            s --;
        printf("\n");
    }
    d=row; 
    for (r = 0; r < row  / 2; r++)
    {
        for (c = 0; c <= d; c++) 
        {
            if (c == d)
                 printf("*");
            else
                 printf(" ");
        }
         printf("\n");
        d++;
    }
   return 0;
}

Output :

Enter row :  5 

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

C++ Code:

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
   int row, r , c ,d,s,w;
   cout << "Enter  rows: ";
   cin >> row;
   d= row -1 ;
    s  = (row  / 3);
    w  = row  / 2 + row  / 5 + s  + s ;
    for (r = 0; r < row ; r++) 
    {
        for (c = 0; c <= w ; c++) 
        {
            if (c == w  -  abs(s) || c ==  abs(s))
                 cout << "*";
            else if ((r == 0 || r == row - 1) && c >  abs(s) && c < w  -  abs(s))
                 cout << "*";
            else
                 cout << " ";
        }
        if (s  != 0 && r < row  / 2) 
            s --;
        else if (r >= (row  / 2 + row  / 5))
            s --;
        cout << "\n";
    }
    d=row; 
    for (r = 0; r < row  / 2; r++)
    {
        for (c = 0; c <= d; c++) 
        {
            if (c == d)
                cout << "*";
            else
                 cout << " ";
        }
         cout << "\n";
        d++;
    }
   return 0;
}
Output :

Enter row :  5 

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

Related Java Star Pattern Programs:

Leave a Comment