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

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) {
  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++ ){
  catch(Exception e){}

Download the program.

Share on Google+Share on Google+

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

Posted on: June 4, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



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
February 12, 2012
The program above!!

Hey dude. The program has a slight error. To properly work, the first loop has to have the condition for (int i=1; i<=a;i++ ). Just thought u should know.
May 22, 2012
my comment

ur examples are not accurate,,,,, u r wasting time of peoples...
June 16, 2012

I need more explanation...!
June 26, 2012
two triangle side by side

hi i need help to make two triangle like (SHALWAR). I MADE IT BUT THE 2ND TRIANGLE IS UNDER FIRST TRIANGLE. PLEASE HELP ME OUT AND EMAIL ME IF YOU CAN THAT WOULD BE HELP. public class Shalwar1 { public static void main(String [] args) { for (int i=1; i<=9; i++) { System.out.println(); for (int j=9; j>=i; j--) { System.out.print(""); System.out.print("*"); } } for ( int x = 1; x <= 10; x++ ) { for ( int y = 1; y < x; y++ ) System.out.print( ' ' ); for (int k = x; k<=10;k++) System.out.print("*"); System.out.println(); } }}
atilade segun o
July 11, 2012

gud day sir, i just want to know if it is possible to become a computer programmer. thank
priyanka chauhan
July 22, 2012
c sharp

how to form a triangle in c sharp with *.by using programing language............
Debasis Sethy
August 31, 2012

solve for java triangle program
September 16, 2012

u wont get the output of stars in triangle form for the above given code
shafinaz islam orthy
December 9, 2011

output- 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 what will be the programme?