Bitsmore360 - Top trends, Tech News, Tips and Tricks: Java Ques 101 - Program to print Floyd's Triangle in Java

Java Ques 101 - Program to print Floyd's Triangle in Java

In this article, "Java Ques 101 - Program to print Floyd's Triangle in Java", we will learn how to create a program that prints a Floyd's Triangle. The pattern-based question is one of the most common questions asked by teachers and interviewers. To understand the program to print Floyd's Triangle, it is important to have some knowledge of patterns.


Java Ques 101 - Program to print Floyd's Triangle in Java

Java Ques 101 - Program to print Floyd's Triangle in Java



What is Floyd's Triangle ? 


Floyd's triangle was named after Robert Floyd. Floyd’s triangle is an arrangement of natural numbers in right angled Triangular form. It starts from number 1 and prints the next greater number in the sequence.





Java Ques 101 - Program to print Floyd's Triangle in Java using For Loop


Here is the source code of java program to print Floyd's Triangle using For loop. In the given program two for loops are used. One for loop is used to print number of rows and other to print number in an incrementing sequence.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class DemoFloyd
{
public static void main(String[] args)
{
int row=5;
int i, j;
int number=1;
for(i=0; i<row; i++)
{
for(j=0; j<=i; j++)
{
System.out.print(number+ " ");
number++;
}
System.out.print("\n");
}
}
}

Output: 



Java Ques 101 - Program to print Floyd's Triangle in Java using While Loop


Here is the source code of java program to print Floyd's Triangle using While loop. In the given program two while loops are used. One while loop is used to print number of rows and other to print number in an incrementing sequence.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class DemoFloyd
{
public static void main(String[] args)
{
int rows=5;
int i=0;
int j=0;
int number=1;
while(i<rows)
{
j=0;
while(j<=i)
{
System.out.print(number+ " ");
number++;
j++;
}
System.out.print("\n");
i++;
}
}
}


Java Ques 101 - Program to print Floyd's Triangle in Java using Scanner class to receive input


Here is the source code of java program to print Floyd's Triangle using Scanner class to receive the input from user.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;

public class DemoFloyd
{
public static void main(String[] args)
{
int number=1;
Scanner sc = new Scanner(System.in);
System.out.println("Floyd's Triangle");      
System.out.println("Enter number of rows: ");
int rows = sc.nextInt();
      
for(int i=0; i<rows; i++)
{
for(int j=0; j<=i; j++)
{
System.out.print(number+ " ");
number++;
}
System.out.print("\n");
}
}
}


Let us see some of the different Pattern questions asked by the teachers and the interviewer-

  • Write a program in java to print a star pattern.
  • Write a program in java to print a number pattern.
  • Write a program in java to print a character pattern.
  • How to print * pattern in java?
  • How to print * pattern in python
  • How to print * pattern in c
  • How to print * pattern in java
  • How to print * pattern in SQL?
  • How to print * pattern in PHP?
  • How to print patterns in Python?
  • How to print patterns in Java?
  • How to print patterns in C?
  • How to print patterns in SQL?
  • Write a program to print half pyramid of numbers
  • Write a program to print inverted half pyramid of *
  • Write a program to print full Pyramid of *
  • Write a program to print Pascal's triangle
  • Write a program to print Floyd's triangle.


Did you find this article helpful? OR Have any doubt, write in the comment section. To learn more about Bitsmore360, check out the rest of our Blog, and visit our Pinterest site and follow us on Instagram.

1 comment: