Java Program to Print Alphabet T Star Pattern

Program to Print Alphabet T Star Pattern

In this article we are going to see how to print the alphabet ‘T’ star pattern, java star pattern programs pdf, character pattern programs in java, number pattern printing in java, pyramid star pattern in java, print star pattern in java, pattern program in java.

Example-1

When size value=8
********
     *  
     *  
     *  
     *  
     *
     *
     *
Example-2:

When size value=14
**************
          *
          *
          *
          *
          *
          *
          *
          *
          *
          *
          *
          *
          *

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 the 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, size=scan.nextInt();
        
        //Outer loop
        for(r = 0; r<size; r++)
        {   
            //Inner loop
            for(c = 0; c<size; c++)
            {
                //Condition to print star
                if(r==0||c==size/2)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }        //Prints a new line
            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,size;
        char inputchar;
        //Taking size as input from user
        Scanner scan = new Scanner(System.in);
        System.out.print("Size : ");
        size=scan.nextInt();
        //Taking random character as input from user
        System.out.print("Character : ");
        inputchar=scan.next().charAt(0);
        //Outer loop
        for(r = 0; r<size; r++)
        {   
            //Inner loop
            for(c = 0; c<size; c++)
            {
                //Condition to print star
                if(r==0||c==size/2)
                    System.out.print(inputchar);
                else
                    System.out.print(" ");
            }        //Prints a new line
            System.out.println();
        }
    }
}
Output

Size : 5
Character : $
$$$$$ 
    $
    $
    $
    $

Explanation :

Let’s understand the program by going through the detailed explanation.

We have taken size value as 5.

Iteration-1
r=0 (passes the first for loop condition) because it will execute until r<size
5 stars will be printed as r==0.

*****

Iteration-2
r=1 (passes the first for loop condition) because it will execute until r<size
Two spaces will be printed here followed by one star as c==size/2.

Then remaining two spaces will be printed.

  *

Iteration-3
r=2 (passes the first for loop condition) because it will execute until r<size
Two spaces will be printed here followed by one star as c==size/2.

Then remaining two spaces will be printed.

  *

Iteration-4
r=3 (passes the first for loop condition) because it will execute until r<size
Two spaces will be printed here followed by one star as c==size/2.

Then remaining two spaces will be printed.

  *

Iteration-5
r=4 (passes the first for loop condition) because it will execute until r<size
Two spaces will be printed here followed by one star as c==size/2.

Then remaining two spaces will be printed.

  *

Now r=5, so first for loop condition will fail.  So next for loop will not be executed any more.

Now, after end of all iteration we will see the complete pattern is printed on the output screen like this.

*****
   * 
   * 
   * 
   *

C Code:

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

    for (r = 0; r < size; r++)
    { //Outer loop
        for (c = 0; c < size; c++)
        { //Inner loop
            if (r == 0 || c == size / 2)
                //Condition to print star
                printf("*");
            else
                printf(" ");
        }
        //Prints a new line
        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 = 0; r < size; r++)
    { //Outer loop
        for (c = 0; c < size; c++)
        { //Inner loop
            if (r == 0 || c == size / 2)
                //Condition to print star
                cout << "*";
            else
                cout << " ";
        }
        //Prints a new line
        cout << endl;
    }
    return 0;
}

Output

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

Answer these:

  1. Write a program to print the following pattern in java
  2. Write a program to print alphabet pattern in java
  3. Java program to print alphabet pattern of stars
  4. Java program to print alphabet pattern
  5. Java program to print alphabet triangle
  6. Java program to print a to z

Related Java Star Pattern Programs: