Floyd's triangle and reverse Floyd's triangle in java
Hello friends,
Let's see what is Floyd's triangle!
The concept of Floyd's Triangle was invented by the computer scientist Robert Floyd.
Floyd's Triangle is a right angled triangle filled by natural numbers, which is having n rows. In this triangle nth row will have n natural numbers. So in the first row there will be only 1 element, that is, 1, in the second row there will be 2 elements next to one, 2 and 3, in third row there will be 3 elements 4,5 and 6. In this way the triangle so formed is called Floyd's Triangle.
1
2 3
4 5 6
7 8 9 10
This is the Floyd's Triangle having 4 rows.
Reverse Floyd's Triangle:-
Now let us see what is Reverse Floyd's Triangle. It is simply reverse of Floyd's Triangle. The natural numbers are written from end to start. For example, the Reverse Floyd's Triangle for 4 rows will be:
10 9 8 7
6 5 4
3 2
1
This is the Reverse Floyd's Triangle having 4 rows.
Here's a program to print Floyd's triangle and reverse Floyd's triangle in Java:-
import java.util.Scanner;
class Floyd
{
public static void main(String args[]) // main class
{
Scanner sc = new Scanner(System.in); //scanner object for user input
System.out.println("Enter the number of rows of Floyd's Triangle: ");
int num = sc.nextInt(); //inputting number of rows in triangle
int i,j,k=0;
System.out.println("\nFloyd's Triangle:\n");
for(i=1;i<=num;i++) //loop for Floyd's Triangle
{
for(j=1;j<=i;j++)
{
k++; //counting natural numbers
System.out.print(k+" ");
}
System.out.println(); //for new row starting
}
System.out.println("\nReverse Floy'd Triangle:");
for(i=num;i>=1;i--) //loop for reverse Floyd's triangle
{
for(j=1;j<=i;j++)
{
System.out.print(k+" ");
k--; //counting natural numbers in reverse
}
System.out.println(); for starting new row
}
}
}
So this is the program for Floyd's Triangle which is written in Java. If you have any queries please post it in the comment box.
Let's see what is Floyd's triangle!
The concept of Floyd's Triangle was invented by the computer scientist Robert Floyd.
Floyd's Triangle is a right angled triangle filled by natural numbers, which is having n rows. In this triangle nth row will have n natural numbers. So in the first row there will be only 1 element, that is, 1, in the second row there will be 2 elements next to one, 2 and 3, in third row there will be 3 elements 4,5 and 6. In this way the triangle so formed is called Floyd's Triangle.
1
2 3
4 5 6
7 8 9 10
This is the Floyd's Triangle having 4 rows.
Reverse Floyd's Triangle:-
Now let us see what is Reverse Floyd's Triangle. It is simply reverse of Floyd's Triangle. The natural numbers are written from end to start. For example, the Reverse Floyd's Triangle for 4 rows will be:
10 9 8 7
6 5 4
3 2
1
This is the Reverse Floyd's Triangle having 4 rows.
Here's a program to print Floyd's triangle and reverse Floyd's triangle in Java:-
import java.util.Scanner;
class Floyd
{
public static void main(String args[]) // main class
{
Scanner sc = new Scanner(System.in); //scanner object for user input
System.out.println("Enter the number of rows of Floyd's Triangle: ");
int num = sc.nextInt(); //inputting number of rows in triangle
int i,j,k=0;
System.out.println("\nFloyd's Triangle:\n");
for(i=1;i<=num;i++) //loop for Floyd's Triangle
{
for(j=1;j<=i;j++)
{
k++; //counting natural numbers
System.out.print(k+" ");
}
System.out.println(); //for new row starting
}
System.out.println("\nReverse Floy'd Triangle:");
for(i=num;i>=1;i--) //loop for reverse Floyd's triangle
{
for(j=1;j<=i;j++)
{
System.out.print(k+" ");
k--; //counting natural numbers in reverse
}
System.out.println(); for starting new row
}
}
}
fig 1: Output of Floyd's Triangle program |
We keep updating our blog with such interesting programs, please follow and share. Thank you!
Mahol Kunal..,
ReplyDeleteThanks @VPS_CORE_LIM.
Delete