# [Java] Floyd’s Triangle Pattern Java Program

In this “Floyd’s Triangle Pattern” – We have written Java programs to print/draw pattern by using different types of Java loops and you can execute and try these Java program through your IDE. This is most [100+] Frequently Asked Java Program on beginner level to check you Java Programming skills and Concept.

## What is Floyd’s Triangle?

Floyd’s triangle is a right arrangements of first natural numbers in triangle form with first natural numbers. All these natural numbers are left to right aligned triangle.

Example: Suppose if no of rows to be displayed is 10 then the desired output should display 10 rows as:

## Algorithms

• Initialization of in memory variable count=1.
• Outer loop will execute based on inserted size
• Inner loop will execute the times number of variable in outer loop
• Print count value inside the inner loop and increase the count by 1 (count++)

Time Complexity: O(n2) for given n

Auxiliary Space: O(1)

## Implementation

This post covers following ways to print Floyd’s Triangle Pattern:

• Floyd’s Triangle Pattern Java Program: Using for Loop
• Floyd’s Triangle Pattern Java Program: Using While Loop
• Floyd’s Triangle Pattern Java Program : Using Do-While Loop

## Floyd’s Triangle Pattern Java Program: For Loop

This Floyd’s Triangle Java Program is following the below logic in method:

```package _1_patterns.Floyd_Triangle;

import java.util.Scanner;

public class FloydTrianglePatternForLoop {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter Pattern Size : ");
int size = sc.nextInt();

drawFloydTrianglePattern(size);

// Close Scanner
sc.close();
}

private static void drawFloydTrianglePattern(int n) {
// Creating and initializing variable for
// rows, columns and display value
int count = 1;
// Outer loop for rows
for (int i = 1; i <= n; i++) {
// Inner loop for columns
for (int j = 1; j <= i; j++) {
// Printing value to be displayed
System.out.print(count + "  ");
// Incremeting value displayed
count++;
}
// Print elements of next row
System.out.println();
}
}
}
```

#### Output

```Enter Pattern Size :
13
1
2  3
4  5  6
7  8  9  10
11  12  13  14  15
16  17  18  19  20  21
22  23  24  25  26  27  28
29  30  31  32  33  34  35  36
37  38  39  40  41  42  43  44  45
46  47  48  49  50  51  52  53  54  55
56  57  58  59  60  61  62  63  64  65  66
67  68  69  70  71  72  73  74  75  76  77  78
79  80  81  82  83  84  85  86  87  88  89  90  91
```

## Floyd’s Triangle Pattern Java Program: While Loop

This Floyd’s Triangle Java Program is following the below logic in method:

```package _1_patterns.Floyd_Triangle;

import java.util.Scanner;

public class FloydTrianglePatternWhileLoop {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter Pattern Size : ");
int size = sc.nextInt();

drawFloydTrianglePattern(size);

// Close Scanner
sc.close();
}

private static void drawFloydTrianglePattern(int n) {
// Creating and initializing variable for
// rows, columns and display value
int i=1, count = 1;

// Outer loop for rows
while (i <= n) {
int j = 1;
// Inner loop for columns
while (j <= i) {
// Printing value to be displayed
System.out.print(count + "  ");
// Incremeting value displayed
count++;
j++;
}
// Print elements of next row
System.out.println();
i++;
}
}
}
```

#### Output

```Enter Pattern Size :
10
1
2  3
4  5  6
7  8  9  10
11  12  13  14  15
16  17  18  19  20  21
22  23  24  25  26  27  28
29  30  31  32  33  34  35  36
37  38  39  40  41  42  43  44  45
46  47  48  49  50  51  52  53  54  55  ```

## Floyd’s Triangle Pattern Java Program: Do-While Loop

This Floyd’s Triangle Java Program is following the below logic in method:

```package _1_patterns.floyd_triangle;

import java.util.Scanner;

public class FloydTrianglePatternDoWhileLoop {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter Pattern Size : ");
int size = sc.nextInt();
drawFloydTrianglePattern(size);

// Close Scanner
sc.close();
}

private static void drawFloydTrianglePattern(int n) {
// Creating and initializing variable for
// rows, columns and display value
int i = 1, count = 1;

// Outer loop for rows
do {
int j = 1;
// Inner loop for columns
do {
// Printing value to be displayed
System.out.print(count + "  ");
// Incremeting value displayed
count++;
j++; // increase loop count
} while (j <= i);
// Print elements of next row
System.out.println();
i++; // increase loop count
} while (i <= n);
}
}
```

#### Output

```Enter Pattern Size :
7
1
2  3
4  5  6
7  8  9  10
11  12  13  14  15
16  17  18  19  20  21
22  23  24  25  26  27  28 ```