Java Program to Print Alphabet C Star Pattern

Program to Print Alphabet C Star Pattern

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

Example-1

For rows = 8

********
*
*
*
*
*
*
********
Example-2

For rows = 5

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

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

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Approach:

  • Enter total row and store it in an integer variable rows.
  • Take one outer for loop to iterate the rows and one inner loop to iterate the columns.
  • Print stars where conditions matches else continue.

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);
    
    //Taking size as input from user
    System.out.print("Rows : ");
    int rows = scan.nextInt();
    
    for (int r = 0; r < rows; r++) //Outer Loop
    { 
        System.out.print("*");
        for (int c = 0; c < rows - 1; c++) //Inner Loop
        { 
            if (r == 0 || r == rows - 1)
                System.out.print("*");
            else
                continue;
        }
        System.out.println();
        //Prints a newline
    }
  }
}

Output

Rows : 5

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

Method-2: User Input Character

import java.util.Scanner;

class Main
{
  public static void main(String[] args) 
  {
    Scanner scan = new Scanner(System.in);
    
    //Taking size as input from user
    System.out.print("Rows : ");
    int rows = scan.nextInt();
    
    //Taking any random character as input from user
    System.out.print("Character : ");
    char any_char = scan.next().charAt(0);
    
    for (int r = 0; r < rows; r++) //Outer Loop
    { 
        System.out.print(any_char);
        for (int c = 0; c < rows - 1; c++) //Inner Loop
        { 
            if (r == 0 || r == rows - 1)
                System.out.print(any_char);
            else
                continue;
        }
        System.out.println();
        //Prints a newline
    }
  }
}

Output:

Rows : 5
Character : c

ccccc
c
c
c
ccccc

Explanation :

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

We have taken rows value as 5.

Iteration-1
r=0, goes into the inner loop prints five stars , one from the outer loop and other four as  r==0 .

*****

Iteration-2
r=1, goes into the inner loop prints one star from the loop iteration.

*

Iteration-3
r=2, goes into the inner loop prints one star from the loop iteration.

*

Iteration-4

r=3, goes into the inner loop prints one star from the loop iteration.

*

Iteration-5

r=4, goes into the inner loop prints five stars , one from the outer loop and other four as  r== rows-1 .

*****

After this r value equals rows i.e. 5 so program will come out of the loop.

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[])
{
    int rows;
    printf("Rows : ");
    scanf("%d", &rows);
    //Taking rows as input from user
    int r, c;
    for (r = 0; r < rows; r++)//Outer Loop
    { 
        printf("*");
        for (c = 0; c < rows - 1; c++) //Inner Loop
        { 
            if (r == 0 || r == rows - 1)
                printf("*");
            else
                continue;
        }
        printf("\n");
        //Prints a newline
    }
    return 0;
}
Output:

Rows : 5

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

C++ Code:

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    int rows;
    cout << "Rows : ";
    cin >> rows;
    //Taking rows as input from user
    int r, c;
    for (r = 0; r < rows; r++)
    { //Outer Loop
        cout << "*";
        for (c = 0; c < rows - 1; c++)
        { //Inner Loop
            if (r == 0 || r == rows - 1)
                cout << "*";
            else
                continue;
        }
        cout << endl;
        //Prints a newline
    }
    return 0;
}

Output:

Rows : 5

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

Related Java Star Pattern Programs: