Saturday, May 28, 2022

How to print a Right Triangle Pattern in Java - Example Tutorial

Hello Java Programmers, here we are back again with a pattern based exercise. In the past, I have shared article on how to print pyramid pattern of stars in JavaLeft Triangle Star Pattern, and Pyramid pattern of alphabets, and in this article, I will show you how to print a right triangle start pattern in Java.  Pattern based exercises are great way to learn programming and coding as it encourage you to use logic and use essential programming constructs like loop and arrays. 


In this tutorial,  you will learn how to write a program to print a right triangle star pattern. It is good to know and understand that in java there are so many things involved in printing out which we shall later look into, We shall first solve the problem before dealing with any other thing that has to do with this.


How to print Right Triangle Star pattern in Java?

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. We shall be doing justice to loops later.

  1. public class StarPrint{
  2. public static void main(String args[]){
  3. int i,j,rows;
  4. Scanner sc = new Scanner(System.in);
  5. System.out.println("Enter the number of rows");
  6. rows = sc.nextInt();
  7. for(i=1;i<=rows;i++)
  8. {
  9. for(j=1;j<=i;j++)
  10. {
  11. System.out.print("* ");
  12. }
  13. System.out.println("");
  14. }
  15. }
  16. }
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 i, j and rows were declared respectively. Note that they can be declared one by one too. you go for anyone you want. 

The scanner class was used here to take user Input after you must have imported it. Then User is been asked how many rows. whatever the user enters would be the rows or the length of the triangle start pattern. Line 7 runs a for loop and inside of it is another loop. then line 11 prints "*" and line 13 prints a new line of space. 

You can see the difference between the prints used in line 11 and line 13. For further explanation, you can view this right-angle star pattern as rows and columns. 

In row 1 a star was printed, in row 2, 2 stars were printed and in row 3, 3 stars were printed. in row 4, there were 4 stars present, and in row 5, 5 stars were there. you can see that the stars increase by one in each row in the loop. so by column, it is the other way round.

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.

How to print a Right Triangle Star Pattern in Java - Example Tutorial


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 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. 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);



That's all about how to print right triangle star patter in Java. You can use this coding exercise to learn more about printing in Java and loops in programming. In this article, you learned how to print a right triangle in java. All areas explained above are important to be able to print patterns in java The "for loop", "while loop", "do-while", and the "print" function as well. 

With these, you can print any kind of pattern of your choice be it rectangle, diamond, square, etc. You can choose any kind of loop as that is the key thing in printing patterns. Just note that the "Do-while" loop executes before the condition is checked so it runs at least once If the conditions eventually turn out to be false!

This is how you print patterns in the java program. With the understanding of loops and the print function, you can print any kind of pattern.

   Other Java Coding Problems for Practice

Thanks for reading this article so far. If you like this solution of printing patterns of star and triangle in Java and my explanation then please share with your friends and colleagues. If you have any questions or feedback then please drop a note. 
   

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.