Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

What is Floyd’s Triangle? How to print Floyd’s Triangle in Java?

What is Floyd's Triangle? How to print Floyd's Triangle in Java?

What is Floyd’s Triangle?

Floyd’s triangle is a triangular array of natural numbers, used in computer science education. Each number in the triangle is smaller than the number below it by the index of its row.

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.

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Floyd’s Triangle

How to print Floyd’s Triangle in Java with Example?

Here is a Java Program to Display Floyd’s Triangle.

Create Java class CrunchifyFloydTriangleTutorial.java

package crunchify.com.java.tutorials;

import java.util.Scanner;

/**
 * @author Crunchify.com
 * What is Floyd's Triangle? How to print Floyd's Triangle in Java?
 */

public class CrunchifyFloydTriangleTutorial {
        public static void main(String[] args) {
                int crunchifyLines;
                int crunchifyStartNumber = 1;
                int crunchifyCounter;
                int crunchifyLoopCounter;
                
                Scanner input = new Scanner(System.in);
                crunchifyPrintResult("How many rows you want to print for Floyd's Triangle? Enter integer number: ");
                
                // Scans the next token of the input as an int.
                // An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix), where radix is the default radix of this scanner.
                crunchifyLines = input.nextInt();
                crunchifyPrintResult("Crunchify's Floyd's triangle result");
                
                crunchifyPrintResult("========================");
                
                // 1st for loop
                for (crunchifyCounter = 1; crunchifyCounter <= crunchifyLines; crunchifyCounter++) {
                        
                        // 2nd for loop
                        for (crunchifyLoopCounter = 1; crunchifyLoopCounter <= crunchifyCounter; crunchifyLoopCounter++) {
                                System.out.print(crunchifyStartNumber + " ");
                                
                                // let's increment counter
                                crunchifyStartNumber++;
                        }
                        //Add new line
                        crunchifyPrintResult("");
                }
                crunchifyPrintResult("========================");
        }
        
        private static void crunchifyPrintResult(String s) {
                System.out.println(s);
        }
}

Just run above program as Java Application in Eclipse console or IntelliJ IDEA to see result as below:

For input: 9

How many rows you want to print for Floyd's Triangle? Enter integer number: 9

Crunchify's Floyd's triangle result
========================
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 
========================

Process finished with exit code 0

For input: 15

How many rows you want to print for Floyd's Triangle? Enter integer number: 
15
Crunchify's Floyd's triangle result
========================
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 
92 93 94 95 96 97 98 99 100 101 102 103 104 105 
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 
========================

Process finished with exit code 0

For input: 5

How many rows you want to print for Floyd's Triangle? Enter integer number: 
5
Crunchify's Floyd's triangle result
========================
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
========================

Process finished with exit code 0

Let me know if you face any issue running above Java program.

The post What is Floyd’s Triangle? How to print Floyd’s Triangle in Java? appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires