This lesson in Java programming will teach you the coding for constructing a shape of triangle by using '*'. First of all make a class named 'triangle' under the Java I/O package and as we have to use the Buffer class, the application of all try and catch block is important for avoiding any kind of error. After creating BufferedReader object and input stream reader define an integer 'a' and apply parseInt method for the conversion of string into integer.
Now apply the for loop and define an integer 'i' and it should be either less than or equal to the integer "a" (the input number). Again define another integer type variable "j" in another for loop. Here in the second for loop "j" the number of times we have to print *.
Now compile and run the program and insert any number on your command window and surely you will get a triangle shape with star *. You can take any other object instead of *.
Here is the code of the program:
Posted on: June 4, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles