Java Program to Print Plus Star Pattern

Write a Java Program to print plus star pattern using for loop. This Java plus star example use if else nested inside a for loop to iterate the rows.

package ShapePrograms;

import java.util.Scanner;

public class PlusPattern1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter Plus Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Plus Pattern of Stars");
		
		for (int i = 1 ; i <= rows * 2 - 1; i++ ) 
		{
			if (i != rows) 
			{
				for (int j = 1 ; j <= rows; j++ ) 
				{
					if(j == rows) {
						System.out.print("*");
					}
					System.out.print(" ");
				}
			}
			else 
			{	
				for (int k = 1 ; k <= rows * 2 - 1; k++ ) 
				{
					System.out.print("*");
				}
			}
			System.out.println();
		}
	}
}
Java Program to Print Plus Star Pattern 1

In this Java Plus pattern of stars program, we replaced the for loop with a while loop.

package ShapePrograms;

import java.util.Scanner;

public class PlusPattern2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter Plus Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Plus Pattern of Stars");
		int i = 1 ;
		while( i <= rows * 2 - 1 ) 
		{
			if (i != rows) 
			{
				int j = 1 ;
				while(j <= rows) 
				{
					if(j == rows) {
						System.out.print("*");
					}
					System.out.print(" ");
					j++;
				}
			}
			else 
			{
				int k = 1;
				while(k <= rows * 2 - 1 ) 
				{
					System.out.print("*");
					k++;
				}
			}
			System.out.println();
			i++;
		}
	}
}
Please Enter Plus Pattern Rows = 7
Printing Plus Pattern of Stars
      * 
      * 
      * 
      * 
      * 
      * 
*************
      * 
      * 
      * 
      * 
      * 
      * 

Java Program to Print Plus Star Pattern using do while loop

package ShapePrograms;

import java.util.Scanner;

public class PlusPattern3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter Plus Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Plus Pattern of Stars");
		int i = 1 ;
		do
		{
			if (i != rows) 
			{
				int j = 1 ;
				do
				{
					if(j == rows) {
						System.out.print("*");
					}
					System.out.print(" ");
				} while(j++ <= rows);
			}
			else 
			{
				int k = 1;
				do
				{
					System.out.print("*");
				} while(++k <= rows * 2 - 1 );
			}
			System.out.println();
		} while( ++i <= rows * 2 - 1 );
	}
}
Please Enter Plus Pattern Rows = 9
Printing Plus Pattern of Stars
        *  
        *  
        *  
        *  
        *  
        *  
        *  
        *  
*****************
        *  
        *  
        *  
        *  
        *  
        *  
        *  
        * 

In this Java example, the plusPattern function prints the Plus pattern of a given symbol.

package ShapePrograms;

import java.util.Scanner;

public class PlusPattern4 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter Plus Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.print("Enter Character for Plus Pattern = ");
		char ch = sc.next().charAt(0);
		
		System.out.println("Printing Plus Pattern of Stars");
		plusPattern(rows, ch);	
	}
	
	public static void plusPattern(int rows, char ch) {
		for (int i = 1 ; i <= rows * 2 - 1; i++ ) 
		{
			if (i != rows) 
			{
				for (int j = 1 ; j <= rows; j++ ) 
				{
					if(j == rows) {
						System.out.print(ch);
					}
					System.out.print(" ");
				}
			}
			else 
			{	
				for (int k = 1 ; k <= rows * 2 - 1; k++ ) 
				{
					System.out.print(ch);
				}
			}
			System.out.println();
		}
	}
}
Please Enter Plus Pattern Rows = 11
Enter Character for Plus Pattern = $
Printing Plus Pattern of Stars
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
$$$$$$$$$$$$$$$$$$$$$
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.