Home Java Beginners Write a program to construct a triangle with the ?*?



Write a program to construct a triangle with the ?*?
Posted on: June 4, 2007 at 12:00 AM
In this section, you will learn how to constructing a shape of triangle by using "*".

Write a program to construct a triangle with the ?*?

     

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: 

import java.io.*;

class triangle{
  public static void main(String[] args) {
  try{
  BufferedReader object = new BufferedReader
(new InputStreamReader(System.in));
  System.out.println("enter the number");
  int a= Integer.parseInt(object.readLine());
  for (int i=1; i<a;i++ ){
  for (int j=1; j<=i;j++ ){
  System.out.print("*");
  }
  System.out.println("");
  }
  }
  catch(Exception e){}
  }
}

Download the program.

Related Tags for Write a program to construct a triangle with the ?*?:
javacstringerrorclassconversionantapplicationimportinputobjectioparsemakeintegermethodversionstreambufferportlockreadnameintriaidblockpackageappdefinefortovoidangleereaderrsiliputusetriangleimfirstapplyinasmntpartrcajpackadafterclcatchbufferedreaderallagenamedoidlocmeintoobjppackcatcreatingtrysbufferedatanypacplyki/oisirhallparseinteaandarstrrtvaufssriringthavstatiapaffefinjeicaicaplndononp


More Tutorials from this section

Ask Questions?    Discuss: Write a program to construct a triangle with the ?*?   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.