Monday, December 2, 2002

Floyd Triangle

package org.interview.test;

import java.util.Scanner;

public class FloydTriangle {

/*
*In Floyd triangle there are n integers in the nth row and a total of (n(n+1))/2 integers 
*in n rows. This is a simple pattern to print but helpful in learning how to create other 
*patterns. Key to develop pattern is using nested loops appropriately.
*/
public static void main (String [] args) throws Exception {
int n, num = 1, c, d;
     Scanner in = new Scanner(System.in);
 
     System.out.println("Enter the number of rows of floyd's triangle you want");
     n = in.nextInt();
 
     System.out.println("Floyd's triangle :-");
 
     for ( c = 1 ; c <= n ; c++ )
     {
        for ( d = 1 ; d <= c ; d++ )
        {
           System.out.print(num+" ");
           num++;
        }
 
        System.out.println();
     }
}
}

No comments:

Post a Comment