Java Tutorial
This java program prints Floyd's triangle using for loop.
Reads the number of rows as user input and prints the required rows as Floyd's triangle.
In Floyd triangle, if there are n elements in n'th row then total number of elements in the triangle is (n(n+!))/2.
import java.util.*; class FloydsTriangle { public static void main(String args[]) { Scanner Sc=new Scanner(System.in); System.out.println("Floyd's Triangle"); System.out.println("-----------------"); System.out.println("Enter the number of rows: "); int rows=Sc.nextInt(); int number = 1; for(int c=1;c<=rows;c++){ for(int d=1;d<=c;d++){ System.out.print(number +"\t"); number++; } System.out.println(); } } }Output:
D:\Java_Programs>javac FloydsTriangle.java D:\Java_Programs>java FloydsTriangle Floyd's Triangle ----------------- Enter the number of rows: 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import java.util.*; class FloydsTriangle { public static void main(String args[]) { Scanner Sc=new Scanner(System.in); System.out.println("Floyd's Triangle"); System.out.println("-----------------"); System.out.println("Enter the number of rows: "); int rows=Sc.nextInt(); int number = 1; int c=1; while(c<=rows){ int d=1; while(d<=c){ System.out.print(number +"\t"); number++; d++; } System.out.println(); c++; } } }Output:
D:\Java_Programs>javac FloydsTriangle.java D:\Java_Programs>java FloydsTriangle Floyd's Triangle ----------------- Enter the number of rows: 6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page