Java

How to Draw a Triangle in Java

Java language provides primitive and non-primitive data types such as “int”, “char”, “Strings”, and much more. It comprises loops, arrays, and lists that are used to perform different types of operations. Java programming language also offers additional functionalities such as drawing different shapes like triangles, patterns, and calculating areas.

This blog will explain the method of drawing triangles in Java. Let’s get started!

How to Draw a Triangle in Java?

A loop is a set of instructions repeatedly executed until a given condition is evaluated as “true”. It is also to draw different types of triangles.

Here, we will use the two loop statements for drawing a triangle in Java:

  • for loop
  • while loop

We will now check out each of the mentioned methods one by one!

Example 1: Draw a Right-angle Triangle in Java Using for Loop

This section will demonstrate the procedure of drawing a triangle using the “for” loop.

To do so, first, we will create integer type variables “size”, “i”, and “j”, where “size” indicates the number of rows of the triangle that will be printed, and “i” and “j” are the iterator variables:

int size =5;
int i,j;

We will use nested “for” loop for drawing a triangle. The first for loop or outer loop will control the rows iteration using the “i” variable, whereas the inner loop is handled using the “j” variable and it will print the triangle using “&” sign:

for (i = 1; i =1; j--)
    {
        System.out.print("&");
     }
        System.out.println(" ");
  }

Execution of the above-given output will display a right-angle triangle:

Example 2: Draw an Isosceles Triangle in Java Using for Loop

In this example, we will draw an isosceles triangle using the “for” loop. Here, we will use three iterator variables, “i”, “j”, and “k”, where “i” is used for rows, “j” is for columns and “k” is used for the printing “@” sign by adding some spaces in rows:

int size=5;
for(int i = 1; i <= size; i++){
    for(int j = 1; j <= size-i; j++){
    System.out.print(" ");
    }
    for(int k = 1; k<= 2*i-1; k++){
        System.out.print("@");
    }
System.out.print("\n");
}

Output

Now, let’s check an example related to drawing a triangle using the while loop.

Example: Draw a Triangle in Java Using while Loop

In this example, we will draw a triangle with the help of the “while” loop. Similar to the above-given example, we have added a nested while loop, with iterator “i” for rows and “j” for columns. The “@” sign is added to give a shape to the triangle. The specified while loop will execute until its condition is evaluated as “true”:

int i,j;
int size =5;
i=1;
while(i<=size){
    j=1;
    while(j<=i){
        System.out.print("@");
        j++;
    }
    i++;
    System.out.println();
}

Output

We have compiled all of the essential information related to drawing a simple triangle in Java.

Conclusion

To draw a triangle in Java, you can utilize a “while” or “for” loop. Java supports the loop statements that help to draw different shapes like triangles, patterns, and others. You can draw any type of shape by using the loop statements. In this blog, we explained the procedure of drawing right-angle and isosceles triangles using for and while loops.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.