Date:

Share:

Diamond Program In Java – Both Full & Half Diamond [2021]

Related Articles

In this tutorial, we will learn about the Diamond program in Java. We have given the example of full diamond patterns and half diamonds alike.

What is a diamond pattern in Java?

A star pattern represented by a diamond is called a Diamond mold And develop a program for representing a diamond pattern This is called a diamond pattern program.

Example:

Read also: Java program for printing movements in a string – 2 simple programs

Simple logic behind a complete diamond plan In Java

First, we will learn the logic of drawing a diamond pattern in Java.

Just as we must display a diamond pattern, so we must display two triangular halves (top half and bottom half) at a time.

We will ask the user to get the number of rows of the diamond pattern he / she wants to display. Once we get the number of lines, we can use that number of lines to display the diamond pattern even more.

We used two different ones For loops Both the upper half and the lower half. For the first top half, we need to print both space and stars at the same time. So we used two different inner loops, one for space and another for stars each time.

Similarly for the bottom half we also used two different inner loops to print both space and star at the same time. For the first half, the number of spaces decreases and in the lower half, the spaces will be printed incrementally.

Let’s see the full program now.

Full diamond program In Java

package cf.java.pattern; import java.util.Scanner; public class FullDiamondPattern public static void main(String[] args) int noOfLines, row, col, noOfspace = 1; System.out.print("Enter the number of rows: "); Scanner scanner = new Scanner(System.in); noOfLines = scanner.nextInt(); noOfspace = noOfLines - 1; System.out.println("Full Diamond Pattern:"); for (col = 1; col <= noOfLines; col++) for (row = 1; row <= noOfspace; row++) System.out.print(" "); noOfspace--; for (row = 1; row <= 2 * col - 1; row++) System.out.print("*"); System.out.println(""); noOfspace = 1; for (col = 1; col <= noOfLines - 1; col++) for (row = 1; row <= noOfspace; row++) System.out.print(" "); noOfspace++; for (row = 1; row <= 2 * (noOfLines - col) - 1; row++) System.out.print("*"); System.out.println("");

Code language: Java (java)

Productivity

Enter the number of rows: 5 Full Diamond Pattern: * *** ***** ******* ********* ******* ***** *** *

Also read: How to convert a list to bytes in Java? The easy way

Simple logic behind a top half diamond program in Java

As we have already seen in the plan above how can we print a full diamond template in Java which is also covered in this top half diamond pattern.

So first of all, we will ask the user to provide the number of rows of the half diamond pattern he / she wants to display. Once we get the number of lines, we can use that number of lines to display the half diamond pattern further.

Here we will use one external for loop and 2 Internal for loops To print the half diamond pattern.

As we explained in the program above, for the top half, we need to print both space and stars at the same time. So we used two different ones Internal for loops One for space and another for stars at a time and will reduce the number of spaces by one in each iteration.

Let’s see the complete plan for printing the top half diamond pattern.

The top half diamond program In Java

package cf.java.pattern; import java.util.Scanner; public class FullDiamondPattern public static void main(String[] args) int noOfLines, row, col, noOfspace = 1; System.out.print("Enter the number of rows: "); Scanner scanner = new Scanner(System.in); noOfLines = scanner.nextInt(); noOfspace = noOfLines - 1; System.out.println("Full Diamond Pattern:"); for (col = 1; col <= noOfLines; col++) for (row = 1; row <= noOfspace; row++) System.out.print(" "); noOfspace--; for (row = 1; row <= 2 * col - 1; row++) System.out.print("*"); System.out.println("");

Code language: Java (java)

Productivity

Enter the number of rows: 5 Full Diamond Pattern: * *** ***** ******* *********

Simple logic behind a bottom half diamond plan

As we have already seen in the first program how can we print a full diamond template in Java which is also covered in this lower half template.

So first of all, we will ask the user to provide the number of rows of the lower half diamond pattern that he / she wants to display. Once we get the number of lines, we can use that number of lines to display the bottom half pattern of the diamond even further.

Here we will use one external for loop and 2 Internal for loops to print the bottom diamond pattern of the half.

As we explained in the plan above, we also used two different ones for the bottom half Internal for loops To print both space and star simultaneously. For the bottom half, spaces will be printed increasingly.

Let’s see the complete plan for printing the pattern of the lower half of the diamond.

Lower diamond half plan In Java

package cf.java.pattern; import java.util.Scanner; public class FullDiamondPattern public static void main(String[] args) int noOfLines, row, col, noOfspace = 1; System.out.print("Enter the number of rows: "); Scanner scanner = new Scanner(System.in); noOfLines = scanner.nextInt(); noOfspace = 1; System.out.println("Full Diamond Pattern:"); for (col = 1; col <= noOfLines - 1; col++) for (row = 1; row <= noOfspace; row++) System.out.print(" "); noOfspace++; for (row = 1; row <= 2 * (noOfLines - col) - 1; row++) System.out.print("*"); System.out.println("");

Code language: Java (java)

Productivity

Enter the number of rows: 5 Full Diamond Pattern: ******* ***** *** *

Other queries we covered

  • Java program for printing diamond printing.
  • How to view diamond pattern in Java?
  • How to print a diamond pattern in Java?
  • How to print a diamond star pattern in Java?

Common questions

What is a diamond pattern?

A star pattern represented by a diamond is called a diamond pattern.

What is Inner for Loop?

One loop for a loop that is declared inside another loop for a loop is called an inner loop.

What is Outer for loop?

One for a loop that contains one or more inner loops is called an outer loop.

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles