Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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) {
    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.

                         

» View all related tutorials
Related Tags: c file class orm constructor object path struct const value name return instance paths create if send match to bot

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

prog for * * * *
* * *
* *
*
code for printing the above triangle

Posted by gyanendra on Thursday, 02.28.08 @ 12:57pm | #50391

Dear sir,
could you explain me clearly about the working of for loop for the above example?

Posted by Preetha on Friday, 01.18.08 @ 09:48am | #45494

hay as the program is meant for printing the
no.of. time a star according to the user input
in the loop u got to put equals only then it
give such output otherwise it gives u wrong
for
eg:
if user input is 5 then 4 star prints
so the loop should be (for int i=0;i<=a;i++)

Posted by Gunaranjan on Monday, 12.17.07 @ 09:15am | #42677

prog for * * * *
* * *
* *
*
code for printing the above triangle.

Posted by chinni on Thursday, 10.18.07 @ 18:29pm | #34336

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.