Preparing for Java Interview?

My books Grokking the Java Interview and Grokking the Spring Boot Interview can help

Download PDF

Thursday, April 20, 2023

How to Print a left triangle star pattern in Java? Example Tutorial

Pattern based exercises are very common on Interviews as they are tricky for beginners and also offers coding practice. In the past, I have shared article on how to print pyramid pattern of stars in Java and Pyramid pattern of albhabets, and in this article, I will show you how to print left triangle star pattern in Java. There are different ways of printing different patterns, but most of them involves using loops and print methods like print() and println() to print characters like star, alphabets or numbers. If you know how to use loops and when to break from loop then you can easily solve pattern based coding problems. In this section, we shall be writing a program to print a left triangle star pattern. We would first implement that before I explain other things that you need to know in getting this task done.

Now, we want to write the codes to print the right triangle star pattern. Before we can print a star pattern in Java programming, first of all, we need two loops. the first one is the outer loop and the second one is the inner loop.

  1. public class LeftStarTriangle {
  2. public static void main(String[] args) {
  3. int row = 10;
  4. for (int i = 1; i <= row ; i++) {
  5. for (int j = 1; j <= row - i; j++) {
  6. System.out.print(" ");
  7. }
  8. for (int k = 1; k <= i; k++) {
  9. System.out.print("*");
  10. }
  11. System.out.println("");
  12. }
  13. }
  14. }
It is obvious that line 1 is the class declaration and line two is the main method. In line 3 variables of type int with the name, "row" were declared. this was kept in the variable so as to keep track of how many spaces we want to print, Line 4 is the for loop, variables of type int with the name "i", which is the counter, initialized to 1 in the loop followed by the counter continuation condition and after that is the increment. 

For as long as the loop condition is true, it keeps executing and increments until false. Then the second loop determines and prints the spaces while the last loop in line 8 prints the star.

It is good to know and understand that in Java, for us to be able to print out something there is a functionality that is behind that which enables us to print whatsoever thing. It is called System.out.print(). out. print method prints the argument that is passed into it.

If I passed"Hello world" as an argument. then, it prints out "Hello World". we won't just be doing only that, we would do justice to loops too. there are different types of loops in java. Namely, For loops, while loops, and do-while loops. Printing of patterns requires iteration and this would make us utilize loops. without a loop(s), it can't be easier. I shall be explaining loops and how it works shortly.

Let us quickly do an overview of the System.out.print:

System – is a final class in java.lang package

out – is a static member of the System class and is the object of PrintStream

println()- is a method of PrintStream that prints whatever is passed to it on the standard output or your screen. To print we need to call println() method, but we can't call this method directly, we need the object of the class to which this method belongs. Hence why we should call it out.print().

There are different ways you can print apart from the introduced one above such as printf(), println(). They print as well too but in a way different from the one initially introduced. printf() is called a formatted print. so, you format it to your taste while println() prints on a new line. 

The difference between print and println is that it is okay to use either print or println on the first line, but if you want your statement on a new line, you have to call on println() method, or else it stays on the same line.



The Printf

This kind of print makes you use any kind of format you want, using a format specifier.

The specifiers are %s, %d, %f.  which represent String, digit, and float or double respectively.

Let me show you an example. Assuming I want to print" Hi 5".

  1. String print = "Hi";
  2. int num = 5;
  3. System.out.printf("%s%2d",print,num);

The above lines of codes print Hi 5.

Note: The 2 before d provides 2 spaces between the Hi and 5

We can print a Java pattern program in different designs. To learn the pattern program, we must have a deep knowledge of the Java loop, such as for loop, while loop, do-while loop as I had said above In this section, we will learn how to print a pattern in Java.

How to Print a left triangle star pattern in Java? Example Tutorial


Fig 1.0 A left-angled star pattern.

Before writing the codes. Lets do Some overviews on each loop, starting with for loop.

For loop

  1. for(int i=1;i<=rows;i++){
  2. ........................
  3. }
A for loop starts by using the keyword "for" followed by a parenthesis.  in the parenthesis, a variable is declared, an int variable because this has to do with numbers followed by the loop continuation condition. In as much the condition remains true it increments hence why we have i++ over there.

While loop
  1. int 1 = 0;
  2. while(i <= 5){
  3. ..........
  4. i++;
  5. }
There is nothing you use for loop for that you can't use while loop for, they both do the same thing. even, do-while. The only difference about do-while is that it runs at least once even if the condition is eventually false, it must have been executed first hence why it is called do-while.
  1. do {
  2. // code block to be executed
  3. }
  4. while (condition);

INFINITE LOOP

Infinite loop as the name implies is the situation whereby the condition never becomes false. The loop keeps running endlessly.


Conclusion

 You learned how to print a left star triangle pattern in java. All areas explained above are important to be able to print patterns in java, The "for loop or while loop", and the print function as well. With this, you can print any kind of pattern of your choice. Be it rectangle, square, diamond, etc.


1 comment :

Anonymous said...

I remember learning coding using these kind of exercises, thanks for memory

Post a Comment