Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

 
 

Share on Google+Share on Google+

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

Advertisement
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.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2007

Related Tutorials

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
 
 
Comments:20
Elina
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.
jimmy
May 22, 2012
my comment

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

I need more explanation...!
irfan
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
programmers

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
Java

solve for java triangle program
swathi
September 16, 2012
comment

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

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