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 Java, Left 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?
- public class StarPrint{
- public static void main(String args[]){
- int i,j,rows;
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter the number of rows");
- rows = sc.nextInt();
- for(i=1;i<=rows;i++)
- {
- for(j=1;j<=i;j++)
- {
- System.out.print("* ");
- }
- System.out.println("");
- }
- }
- }
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.
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".
- String print = "Hi";
- int num = 5;
- 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
- for(int i=1;i<=rows;i++){
- ........................
- }
- int 1 = 0;
- while(i <= 5){
- ..........
- i++;
- }
- do {
- // code block to be executed
- }
- 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.
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.
- how to multiply matrix in Java
- How to calculate the square root of a given number in Java
- How to reverse words in a given String in Java
- How to check if two rectangles intersect with each other in Java?
- How to print Fibonacci series in Java
- How to check if two given Strings are Anagram in Java
- How to count vowels and consonants in given String in Java
- How to check if given number is prime in Java
- How to check if given String is palindrome or not in Java
- How to find all permutations of a given String in Java
- How to reverse a String in place in Java
- How to remove duplicate elements from the array in Java
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.