[Java] Rhombus Pattern Java Program


In this “Print Rhombus Pattern” – We have written Java programs to print/draw Rhombus pattern by using different types of Java loops and you can execute and try these Java program through your IDE. This is most [100+] Frequently Asked Java Program on beginner level to check you Java Programming skills and Concept.

Pattern Example

This post covers following ways to print Rhombus Pattern:

  • Print Rhombus Pattern Java Program: Using for Loop
  • Print Rhombus Pattern Java Program: Using While Loop
  • Print Rhombus Pattern Java Program: Using Do-While Loop

Rhombus Pattern Java Program: For Loop

This Rhombus Java Program is following the below logic:

  1. In this program ask inputs from the user for size of rhombus pattern and symbol to create it.
  2. Outer for loop is used to print number of rows while inner for loops are used to print number of columns.
  3. First inner for loop (int j = 1; j <= n – i; j++) is used to print spaces and it reduce the number of spaces bases on each count of outer loop.
  4. Second inner for loop (int j = 1; j <= n; j++) print the number symbols as entered in size.
package _1_patterns.rhombus;

import java.util.Scanner;

public class RhombusPatternForLoop {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Pattern Size : ");
		int size = sc.nextInt();

		System.out.print("Enter Symbol : ");
		char symbol = sc.next().charAt(0);

		drawRhombusPattern(size, symbol);

		// Close Scanner
		sc.close();
	}

	private static void drawRhombusPattern(int n, char c) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n - i; j++)
			{
				System.out.print(" ");
			}
			for (int j = 1; j <= n; j++)
			{
				System.out.print(c);
			}
			System.out.println();
		}	
	}
}

Output

Enter Pattern Size : 
6
Enter Symbol : $
     $$$$$$
    $$$$$$
   $$$$$$
  $$$$$$
 $$$$$$
$$$$$$

Rhombus Pattern Java Program: While Loop

This Rhombus Java Program is following the below logic:

  1. Outer while loop is used to print number of rows while inner while loops are used to print number of columns.
  2. First inner while loop (j++ <= n-i) is used to print spaces and it reduce the number of spaces bases on each count of outer loop.
  3. Second inner while loop (j++ <= n) print the number symbols as entered in size.
package _1_patterns.rhombus;

import java.util.Scanner;

public class RhombusPatternForLoop {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Pattern Size : ");
		int size = sc.nextInt();

		System.out.print("Enter Symbol : ");
		char symbol = sc.next().charAt(0);

		drawRhombusPattern(size, symbol);

		// Close Scanner
		sc.close();
	}

	private static void drawRhombusPattern(int n, char c) {
	int i = 1;
		while (i <= n) {
			int j = 1;
			while (j++ <= n - i) {
				System.out.print(" ");
			}
			j = 1;
			while (j++ <= n) {
				System.out.print(c);
			}
			System.out.println();
			i++;
		}
	}
}

Output

Enter Pattern Size : 
7
Enter Symbol : #
      #######
     #######
    #######
   #######
  #######
 #######
#######

Rhombus Pattern Java Program: Do-While Loop

This Rhombus Java Program is following the below logic:

  1. Outer dowhile loop is used to print number of rows while inner do-while loops are used to print number of columns.
  2. First inner do-while loop (j++ <= n-i) is used to print spaces and it reduce the number of spaces bases on each count of outer loop.
  3. Second inner do-while loop (j++ <= n) print the number symbols as entered in size.

package _1_patterns.rhombus;

import java.util.Scanner;

public class RhombusPatternForLoop {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter Pattern Size : ");
    int size = sc.nextInt();

    System.out.print("Enter Symbol : ");
    char symbol = sc.next().charAt(0);

    drawRhombusPattern(size, symbol);

    // Close Scanner
    sc.close();
}

private static void drawRhombusPattern(int n, char c) {
int i = 1;
		do {
			int j = 1;
			do {
				System.out.print(" ");
			} while (j++ <= n - i);
			j = 1;
			do {
				System.out.print(c);
			} while (++j <= n);

			System.out.println();
			i++;
		} while (i <= n);
}
}

Output

Enter Pattern Size : 
10
Enter Symbol : ^
          ^^^^^^^^^^
         ^^^^^^^^^^
        ^^^^^^^^^^
       ^^^^^^^^^^
      ^^^^^^^^^^
     ^^^^^^^^^^
    ^^^^^^^^^^
   ^^^^^^^^^^
  ^^^^^^^^^^
 ^^^^^^^^^^

Hope this post helps you to implement the Rhombus Pattern through Java Program. Please share your comments.

Happy Learning!!!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s