Java Program to Print Letter B Star Pattern

Program to Print Letter B with Star Pattern

In this article we are going to see how to print star pattern of letter B.

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

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

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.
  • 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; 
     
// Make instance of Scanner class and take inputs
    Scanner sc= new Scanner(System.in);
    System.out.print("Enter no of row = ");
    row=sc.nextInt();
    System.out.print("Enter no of columns = ");
    column=sc.nextInt();
    
//Getting middle of B
    int row_mid=row/2;

// iterate outer loop from r=1 till row
    for (r = 1; r <= row; r++) 
    {
        for (c = 1; c <= column; c++) 
        {
    // if r= 1 or row_mid or row print the symbol
        	if(r == 1 || r == row_mid || r == row ) 
        	{
        	    System.out.print("*");
            }
            // if c=2 or column print the symbol
            else if(c == 2 || c == column ) 
            {
        		System.out.print("*");	
            } 
            else 
            // otherwise print a space	
            {
                System.out.print(" ");	
            }
        }
        System.out.println("");
    }
    
    }
}
    

Output:

Enter no of row = 11
Enter no of columns = 10
**********
 *           *
 *           *
 *           *
 *           *
**********
 *           *
 *           *
 *           *
 *           *
**********

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; 
     
// Make instance of Scanner class and take inputs
    Scanner sc= new Scanner(System.in);
    System.out.print("Enter no of row = ");
    row=sc.nextInt();
    System.out.print("Enter no of columns = ");
    column=sc.nextInt();
    System.out.print("Enter any character = ");
    char s=sc.next().charAt(0);
    int row_mid=row/2;

// iterate outer loop from r=1 till row
    for (r = 1; r <= row; r++) 
    {
        for (c = 1; c <= column; c++) 
        {
    // if r= 1 or row_mid or row print the symbol
        	if(r == 1 || r == row_mid || r == row ) 
        	{
        	    System.out.print(s);
            }
            // if c=2 or column print the symbol
            else if(c == 2 || c == column ) 
            {
        		System.out.print(s);	
            } 
            else 
            // otherwise print a space	
            {
                System.out.print(" ");	
            }
        }
        System.out.println("");
    }
    
    }
}
    

Output:

Enter no of row = 11
Enter no of columns = 10
Enter any character = b
bbbbbbbbbb
  b               b       
  b               b       
  b               b       
bbbbbbbbbb
  b               b       
  b               b       
  b               b 
  b               b
bbbbbbbbbb

C Code:

#include <stdio.h>	

int main()
{
    
    int r, c; 
    int row;
    int column; 
    int row_mid=6; 
    printf("Enter no of row = ");
    scanf("%d",&row);
    printf("Enter no of columns = ");
    scanf("%d",&column);


    for (r = 1; r <= row; r++) {
        for (c = 1; c <= column; c++) {
        	if(r == 1 || r == row_mid || r == row ) {
        		printf("*");
            } else if(c == 2 || c == column ) {
        		printf("*");	
            } else {
                printf(" ");	
            }
        }
        printf("\n");
    }
    
   return 0;
}
Output:

Enter no of row = 11
Enter no of columns = 10
**********
  *          *
  *          *
  *          *
  *          *
**********
  *          *
  *          *
  *          *
  *          *
**********

C++ Code:

#include <iostream>
using namespace std;

int main()
{
    
    int r, c; 
    int row;
    int column; 
    int row_mid=6; 
    cout<<"Enter no of row = ";
    cin>>row;
    cout<<"Enter no of columns = ";
    cin>>column;


    for (r = 1; r <= row; r++) {
        for (c = 1; c <= column; c++) {
        	if(r == 1 || r == row_mid || r == row ) {
        		cout<<"*";
            } else if(c == 2 || c == column ) {
        		cout<<"*";	
            } else {
                cout<<" ";	
            }
        }
        cout<<"\n";
    }
    
   return 0;
}
Output:

Enter no of row = 11
Enter no of columns = 10
**********
  *          *
  *          *
  *          *
  *          *
**********
  *          *
  *          *
  *          *
  *          *
**********

Related Java Star Pattern Programs: