Floyd Triangle Program in Java

Welcome back again, today at Java Code Online, I would be discussing the Java code for Floyds Triangle.

The Floyds triangle starts from 1, it is a right angled triangle, consisting of consecutive numbers. If suppose four rows of the Floyds Triangle need to be generated, then the output will be, the first row contains 1, the second row contains 2 3, the third row contains 4 5 6, and the last row contains 7 8 9 10. I hope that will clear your concept about the Floyds Triangle. One more intresting fact about this triangle is that all the last numbers of each row are Triangular numbers.

The Java Code provided, asks the user to enter the number of rows till which the Floyds Triangle is desired, and then generates it. The Java Program is given below:-

package developer;

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

public class FloydTriangle {

static int counter = 0;
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
System.out.println("Enter the number of rows for Floyd Triangle:");
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);


int num = Integer.parseInt(br.readLine());

for(int i = 1; i <= num; i++)
{
for(int j = 1; j <= i; j++)
{
counter = counter + 1;
System.out.print(counter);
System.out.print(" ");
}
System.out.println("");
}

}
}

I hope that this post was helpful to you all. Kindly leave your comments in case you liked the above code. For more info on Java, keep buzzing Java Code Online.

Related Posts by Categories

0 comments:

Post a Comment

Blog Archive

Powered by Blogger.