Java Program to Print Digit 2 Star Pattern

Program to Print Digit 2 Star Pattern

In this article we are going to see how to print the star pattern of digit 2.

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

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 total row and store it in an integer variable say row.
  • Enter total column and store it in an integer variable say column.
  • Enter size of row and store it in an integer variable say row_length.
  • Take a inner loop to print the column values.
  • Then go on printing the star symbols according to the iteration.

JAVA Code:

Method-1 : Static Star Character

import java.util.Scanner;
public class Main{
    

public static void main(String[] args)
{
    
    int r, c; 
    int row;
    int column; 
    int row_length; 
    Scanner sc= new Scanner(System.in);
    System.out.print("\nEnter no of rows = ");
    row=sc.nextInt();
    System.out.print("\nEnter no of columns = ");
    column=sc.nextInt();
    System.out.print("\nEnter size of row = ");
    row_length=sc.nextInt();
    
    // For no of rows 
    for (r = 1; r <= row; r++) 
    {
        // Print the column values
        for (c = 1; c <= column; c++) 
        {
            //print the symbols for a row
        	if(r == 1 || r == row_length || r == row ) 
        	{
        		System.out.print("*");
        	// print symbols from column to row_length
            } else if(c == column && r <= row_length ) {
        		System.out.print("*");	
        	// print symols from 1 till row_length-1
            } else if(c == 1 && r > row_length) {
        		System.out.print("*");	
            } else {
                System.out.print(" ");	
            }
        	
        }
        System.out.println("");
    }
    

}
}
Output:

Enter no of rows = 10
Enter no of columns = 9
Enter size of row = 5
*********
            *
            *
            *
*********
*       
*       
*       
*       
*********

Method-2 : User Input Character

import java.util.Scanner;
public class Main{
    

public static void main(String[] args)
{
    
    int r, c; 
    int row;
    int column; 
    int row_length;
    char s;
    Scanner sc= new Scanner(System.in);
    System.out.print("\nEnter no of rows = ");
    row=sc.nextInt();
    System.out.print("\nEnter no of columns = ");
    column=sc.nextInt();
    System.out.print("\nEnter size of row = ");
    row_length=sc.nextInt();
    System.out.print("\nEnter random character = ");
    s=sc.next().charAt(0);
    
    // For no of rows 
    for (r = 1; r <= row; r++) 
    {
        // Print the column values
        for (c = 1; c <= column; c++) 
        {
            //print the symbols for a row
        	if(r == 1 || r == row_length || r == row ) 
        	{
        		System.out.print(s);
        	// print symbols from column to row_length
            } else if(c == column && r <= row_length ) 
            {
        		System.out.print(s);	
        	// print symols from 1 till row_length-1
            } 
            else if(c == 1 && r > row_length) 
            {
        		System.out.print(s);	
            } else 
            {
                System.out.print(" ");	
            }
        	
        }
        System.out.println("");
    }
    

}
}
Output:

Enter no of rows = 10
Enter no of columns = 9
Enter size of row = 5
Enter random character = 2
222222222
                2
                2
                2
222222222
2       
2     
2       
2       
222222222

C Code:

#include <stdio.h>	

int main()
{
    
    int r, c; 
    int row;
    int column; 
    int row_length; 
    printf("\nEnter no of rows = ");
    scanf("%d",&row);
    printf("\nEnter no of columns = ");
    scanf("%d",&column);
    printf("\nEnter size of row = ");
    scanf("%d",&row_length);
    
    for (r = 1; r <= row; r++) 
    {
        for (c = 1; c <= column; c++) 
        {
        	if(r == 1 || r == row_length || r == row ) 
        	{
        		printf("*");
            } else if(c == column && r <= row_length ) 
            {
        		printf("*");	
            } else if(c == 1 && r > row_length) 
            {
        		printf("*");	
            } else {
                printf(" ");	
            }
        	
        }
        printf("\n");
    }
    
   return 0;
}


Output:

Enter no of rows = 10
Enter no of columns = 9
Enter size of row = 5
*********
            *
            *
            *
*********
*       
*       
*       
*       
*********

C++ Code:

#include <iostream>	
using namespace std;
int main()
{
    
    int r, c; 
    int row;
    int column; 
    int row_length; 
    cout<<"Enter no of rows = ";
    cin>>row;
    cout<<"Enter no of columns = ";
    cin>>column;
    cout<<"Enter size of row = ";
    cin>>row_length;
    
    for (r = 1; r <= row; r++) 
    {
        for (c = 1; c <= column; c++) 
        {
        	if(r == 1 || r == row_length || r == row ) 
        	{
        		cout<<"*";
            } else if(c == column && r <= row_length ) 
            {
        		cout<<"*";	
            } else if(c == 1 && r > row_length) 
            {
        		cout<<"*";	
            } else {
                cout<<" ";	
            }
        	
        }
        cout<<"\n";
    }
    
   return 0;
}


Output:

Enter no of rows = 10
Enter no of columns = 9
Enter size of row = 5
*********
            *
            *
            *
*********
*       
*       
*       
*       
*********

Related Java Star Pattern Programs: