Home Java Beginners Write a program to construct a triangle with the ?*?
Questions:Ask|Latest


 
 

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

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

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
 
 
Comments
chandrasekhar
June 16, 2011
thanks lot

Thank u so much for providing coding and explanation for programs it helped me lot.. I hope that u will give more programs for us .. Regards, chandrasekhar reddy
Mukund Potdar
August 14, 2011
Star program in Java

There some error while running i.e "Exception in thread "main" java.lang.NoClassDefFoundError: Triangle <wrong name: Triangle>
kusum sorout
August 27, 2011
java programs

methods for creating different types of shapes using stars'*'.
Bongani
August 29, 2011
question

how to create a c# program that will prompt the user to enter a number; this will be the size of a triangle. then call the function draw iscosceles triangle, using try catch statement?
Nishikant
September 4, 2011
java

how can you print in java programming in this serig * * * * * * * * * *
vinodkumar
September 6, 2011
above tringle prgm

its will throws the exception so we have to put throws IOException
mark
October 5, 2011
program

hello... i'm in need of your help.. pleaase help me with this program...using simple linear array.... Create a program that will accept 20 numbers and arrange them in ascending order. The program should determine the distinct numbers and the number of occurrences for each number (or the frequency). The program should also determine the mean, median and the mode of the inputs. Display the supplied values, the distinct numbers, the frequency of each distinct numbers, mean, median and mode. Your program should use three arrays that will hold the entered numbers, distinct numbers, and the frequency, respectively. Use the following numbers in your first run: 1 37 25 1 56 8 6 9 11 8 54 56 20 25 1 8 6 20 56 56 Enter 20 numbers: 1 37 25 1 56 8 6 9 11 8 54 56 20 25 1 8 6 20 56 56 Your entered numbers are: 20 37 25 1 56 8 6 9 11 23 54 56 20 25 1 8 56 20 56 56 Your sorted numbers are: 1 1 1 6 6 8 8 8 9 11 20 20 25 25 37 54 56 56 56 56 Distinct Number Frequency 1 3 37 1 25 2 56 4 8 3 6 2 9 1 11 1 54 1 20 2 Mean: 23.2 Median: 15.5 Mode: 56 AND THIS.... using simple two dimensional array.. Create a program that displays that accepts a month number and a year. The program should display the monthly calendar for a particular year which contain the Month, days, and the year. Take note also the leap years. Example: Enter a month number: 2 Enter a year: 2008 Your monthly calendar is: February 2008 Su Mo Tue Wed Thur Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 12 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 thanks...favor... can you help me ASAP thanks.......More power...:D
ch ramanatha gopala reddy
February 16, 2013
please write triangle program using collections

if anybody write this one.... very nice site
fadele funmilayo
October 19, 2011
emmergency pls

Can anyone pls write d code in either pascal or q basic for me. The code ? draw both a triaaqngle amd a rectangle. Thanks!
Sanjay
October 28, 2011
program information

i want complete information about java programing. plese send me program information. thanx
Sadun
November 30, 2011
Java

Thanks.Very usefull me
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