Friday, 3 January 2014

Write a program to print a Floyd's Triangle

Floyd's Triangle:

Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:2 34 5 67 8 9 10 and so on....

Program:

import java.util.*;
class FloydsTriangle 
{
public static void main(String[] args)
{
int limit; //this variable is useful for howmany  rows we need?
System.out.println("Enter the limit: ");// asking for limit
Scanner sc=new Scanner(System.in);//taking the input from console
limit=sc.nextInt();//taking the input as integer
int count=1; // for printing purpose
for (int index=1;index<=limit ;index++ ) //this is for number of rows
{
  for (int index1=1;index1<=index ;index1++ ) //in particular row it displays the howmany numbers u want
  {
      System.out.print(count+" "); //printing the number by using the "print" method
  count++;// count increment
  
  }//inner for loop
  System.out.println("");// it is for nextline 
           
            }//outer for loop
}//main
}//class

Output:


C:\Documents and Settings\user1\Desktop\Programs>javac FloydsTriangle.java

C:\Documents and Settings\user1\Desktop\Programs>java FloydsTriangle
Enter the limit:
4
1
2 3
4 5 6
7 8 9 10


3 comments:

Unknown said...

very nice logic i have never seen this kind of smart logic.....

Unknown said...

great logic Ajay, carry on

Unknown said...

thank you Vivek and Narayana