Java program that prints a triangle
Can anyone help? I have this so far. Hint: For each row, you need to first print some spaces and then print some stars. The number of spaces should decrease by one per row, while the number of stars should increase.
Ilmari Karonen has good advice, and I'd just like to generalize it a bit. In general, before you ask "how can I get a computer to do this? So, if someone gave you an empty Word document and asked you to create the triangles, how would you go about doing it? Whatever solution you come up with, it's usually not hard to translate it to Java or any other programming language.
It might not be the best solution, but hopefully! So for instance, maybe you would say that you'd type out the base, then go up a line, then type the next highest line, etc. That suggests that you can do the same in Java -- create a list of Strings, base-to-top, and then reverse them.
That might suggest that you can just create them in reverse order, and then not have to reverse them. And then that might suggest that you don't need the list anymore, since you'll just be creating and printing them out in the same order -- at which point you've come up with essentially Ilmari Karonen's advice.
Or, maybe you'd come up with another way of doing it -- maybe you'd come up with Ilmari Karonen's idea more directly. Regardless, it should help you solve this and many other problems. NO of spaces in each row at start is decremented by 1.
So for Right alinged triangle we need to use 3 for loop. The spaces on the left of the triangle will automatically adjust itself depending on what character or what word you want to print. Note: i is the row index from 0 to rowNumber and rowNumber is number of rows. This is the least complex program, which takes only 1 for loop to print the triangle.
This works only for the center triangle, but small tweaking would make it work for other's as well -. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? In this pattern we will be printing numbers with rows count, for example, 1 will be printed 1 time, 2 will be printed 2 times and so on.
I have explained main loops of code using comments, read them to understand the output more clearly. Simple modification of "pattern 1" program, now we are printing varibale "j" instead of "i", so output will be as below.
In this pattern we will be printing first "pattern 2" and then "reverse of pattern 2". That's it, hope you have understood the concepts of looping in java, feel free to post your comments on this article.
How to compara Strings in Java? Fibonacci series program in Java With and without recursion. Java ArrayList. Popular Examples Check prime number. Print the Fibonacci series. Print Pyramids and Patterns. Multiply two matrices. Find the standard deviation.
Reference Materials String. Start Learning Java. Explore Java Examples. Once you mastered these we would suggesting looking at the example covering how to generate a Pascals Triangle. Name required. Mail will not be published required.
Featured , Java Full source code for all the problems discussed above is available for download to our members. November 23, at pm.
0コメント