Java Program to Print Floyd’s Triangle

«
»



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

In Floyd triangle there are n integers in the nth row and a total of (n(n+1))/2 integers in n rows. We take input as number of rows user want to print.

Here is the source code of the Java Program to Display Floyd’s Triangle. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2.  
  3. class Floyd
  4. {
  5.    public static void main(String args[])
  6.    {
  7.       int n, num = 1, c, d;
  8.       Scanner in = new Scanner(System.in);
  9.       System.out.println("Enter the number of rows of floyd's triangle you want");
  10.       n = in.nextInt();
  11.       System.out.println("Floyd's triangle :-");
  12.       for ( c = 1 ; c <= n ; c++ )
  13.       {
  14.          for ( d = 1 ; d <= c ; d++ )
  15.            {
  16.               System.out.print(num+" ");
  17.               num++;
  18.            }
  19.           System.out.println();
  20.       }
  21.    }
  22. }

Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
$ javac Floyd.java
$ java Floyd
 
Enter the number of rows of floyd's triangle you want
5
Floyd's triangle :-
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

Check this: BCA MCQs | Programming MCQs

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.