Thursday, 23 April 2015

Floyd's Triangle

import java.io.InputStreamReader;
import java.io.BufferedReader;

public class eFloydsTraingle {
    public static void main(String args[]) {
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(in);

        int length = 0;
        // get a number
        System.out.println("Enter height of triangle : ");
        try {
            length = Integer.parseInt(reader.readLine());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Out of memory");
            return;
        }
       
        int fIndex = 1 ;
        // print the triangle
        for(int i=0 ; i < length ; ++i)
        {
            for(int j= 0 ; j <= i ; ++j){
                System.out.print(fIndex++  + " ");
            }
            System.out.println();
        }

    }
}

No comments:

Post a Comment