Wednesday 9 January 2019

Write a Java program to print the Floyd's triangle

      In the current post, I have written java code to print or display the Floyd's Triangle. The Floyd's triangle is a right angled triangular of natural array. The numbers are consecutive and natural numbers, start with 1 at the top of left corner.

See the below code,

FloydsTriangle.java,


package com.example;

class FloydsTriangle {

       public static void main(String args[]) {
       
              int noOfrows = 4;  // this is the number of rows.
              int number = 1;
       
              System.out.println("Floyd's triangle Example");
              System.out.println("---------------------------");
             
              for (int count = 1 ; count <= noOfrows ; count++ ) {
                     
                     for (int j = 1 ; j <= count ; j++ ) {
                            
                            System.out.print(number+" ");
                            
                            number++;
                     }

                   System.out.println();  // next line or new line

              }
       }
}

Output :--

Floyd's triangle Example
---------------------------

2 3 
4 5 6 
7 8 9 10


Related Posts:--
1) Java Program for Bubble Sort in Ascending and Descending order
2) Java Program to Reverse a Linked List using Recursion and Loops
3) How to Remove duplicates from ArrayList in Java
4) Java Program to Count Occurrence of Word in a Sentence
5) Java Object sorting example (Comparable and Comparable)

No comments:

Post a Comment