In this post, I have written a Java program to print Floyd's Triangle. Floyd's Triangle is a right-angled triangular arrangement of consecutive natural numbers, starting with 1 at the top-left corner.
See the code below.
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
---------------------------
1
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