Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*
- Write a program to generate following pyramid pattern. Use for loop
- to generate the pyramid/triangle.
- *
- **
- ***
- ****
- *****
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Pyramid1 {
- public static void main(String args[]){
- try{
- System.out.println("Enter number of lines to generate : ");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int numberOfLines = Integer.parseInt(br.readLine());
- for(int i=1; i <= numberOfLines; i++){
- for(int j=0; j<i ; j++){
- System.out.print("*");
- }
- //generate new line
- System.out.println("");
- }
- }catch(NumberFormatException nfe) {
- System.out.println("Invalid number." + nfe);
- }catch(IOException ioe){
- System.out.println("IOException " + ioe);
- }
- }
- }

Post new comment