Home > Java > javaTutorial > Java program to display Floyd triangle

Java program to display Floyd triangle

王林
Release: 2023-09-15 08:05:02
forward
1203 people have browsed it

Java program to display Floyd triangle

Floyd's triangle is a popular right triangle array composed of natural numbers. Its name comes from its founder, Robert W. Floyd, a famous computer scientist. The top of the triangle is the number 1, and then each subsequent number is incremented by 1 as you move down each row.

In this article, we will see how to display Floyd's triangle using a Java program.

But before moving on to Java implementation, let’s take a deeper look at Freud’s triangle.

The first row contains only one number, 1 itself, and each subsequent row has one more number than the previous row. A triangle has n rows, where n can be any positive integer.

The total number of values ​​in the triangle will be the sum of the 1st n natural numbers which is calculated using the formula S = n/2 * (2a (n-1) d) where S is the sum of the series , n is the number of terms in the series, a is the first term in the series, d is the common difference between the terms.

However, in a Floyd’s triangle, the 1st term is always 1 and the common difference is 1 so we can simplify this formula to:

S= n/2 * (2 + n – 1) = n/2 * (n+1)
Copy after login

Therefore, the total number of values ​​in n rows in Floyd's triangle is n/2 * (n 1).

If there are 5 rows, that is, n=5, then the total number of values ​​in the triangle is:

5/2 * (5+1) = 15
Copy after login

algorithm

Input: Number of rows n

  • 1. Initialize a variable "number" to 1

  • 2. For i from 1 to n, do the following −

    • a. For j from 1 to i, do the following -

      • i. Print the value of "number"

      • ii. Increment "number" by 1

    • b. Print a newline character to move to the next line

Use for loop

For loops are a type of control flow statement that executes a set of instructions repeatedly. It contains 3 parts which are an initialization statement, a Boolean condition, and an update statement. After the loop body is executed the update statement is executed and the condition is checked again until the Boolean condition becomes false.

The java implementation to display Floyd’s triangle using nested for loops is given below.

Example

public class FloydTriangle {
   public static void main(String[] args) {
      // declare the number of rows 
      int rows = 5;
      int num = 1;
   
      for (int i = 1; i <= rows; i++) {
         for (int j = 1; j <= i; j++) {
            System.out.print(num + " ");
            num++;
         }
         System.out.println();
      }
   }
}
Copy after login

Output

The above program will produce the following output -

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15
Copy after login
Copy after login

Using while loops

While loops are another form of control flow statements which repeatedly execute based up on a pre-defined Boolean condition and terminates itself once the condition is false.

Example

public class FloydTriangle {
   public static void main(String[] args) {
      int rows = 5;
      int number = 1;
      int i = 1;
   
      while (i <= rows) {
         int j = 1;
         while (j <= i) {
            System.out.print(number + " ");
            number++;
            j++;
         }
         System.out.println();
         i++;
      }
   }
}
Copy after login

Output

The above program will produce the following output -

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15
Copy after login
Copy after login

Using Do-While Loop

The do while loop is very similar to the while loop except that it executes at least once because the condition is tested at the end of each iteration. The loop continues execution if the condition is true and terminates if the condition is false. The number of rows is predefined here as 10.

Example

public class FloydTriangle {
   public static void main(String[] args) {
      int rows = 10;
      int number = 1;
      int i = 1;
   
      do {
         int j = 1;
         do {
            System.out.print(number + " ");
            number++;
            j++;
         } while (j <= i);
         System.out.println();
         i++;
      } while (i <= rows);
   }
}
Copy after login

Output

The above program will produce the following output -

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
Copy after login

in conclusion

The Freudian Triangle is a simple example used to demonstrate and practice basic concepts such as cycles and patterns. It is not limited to Java implementation, it is commonly used to teach many programming languages ​​such as C, Java, C#, etc. A triangle consists of n rows, where n can be predefined while writing the code and stored in an integer. It can be further optimized to ask the user to enter the value of n or the number of rows (using the help of Scanner class or any other input method), which will give better practice to the learner. Overall, this program is a simple and efficient way to generate Freudian triangles in Java. Be careful when defining the conditions for the loop to prevent entering an infinite loop.

The above is the detailed content of Java program to display Floyd triangle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template