Floyd’s triangle Number pattern using nested for in Java
In this tutorial, we will learn about Floyd’s triangle Number pattern using nested for in Java language
Nested loops are(for, while,do-while) useful to create funny patterns as number patterns alphabet, Star patterns
We will learn in this tutorial about Triangle number pattern printing using nested for in Java
- write this program of your text editor
- save your file name like as class name and .java extension
- compile the program using Javac compiler
- finally, run the program (using .class file)
Program 1
package floyestrangle; public class Floyestrangle { public static void main(String[] args) { int rows=7;//variable declaration and inilization for(int i=1; i<=rows; i++){//nested for loop for(int j=1; j<=i; j++){ System.out.print(j); } System.out.println(); } } }
When the above code is executed, it produces the following results:
1 12 123 1234 12345 123456 1234567
you can add some appropriate changes in this program
below we can try to some small changes
package floyestrangle; import java.util.Scanner; //import scanner package public class Floyestrangle { public static void main(String[] args) { System.out.println("welcome to print Floyd's triangle in java"); //add a description System.out.println("please enter a number of rows :"); Scanner sc=new Scanner(System.in); //use input method int rows=sc.nextInt(); //get input from user to print Floyd's triangle for(int i=1; i<=rows; i++){ for(int j=1; j<=i; j++){ System.out.print(j +" ");//concatenate space in java //get small space among the numbers } System.out.println(); } } }
In line 2- import scanner package
In line 5 – add a description
In line 7 – Use input method
In line 8 -get input from the user to print Floyd’s triangle
In line 11 – concatenate spaces
When the above code is executed, it produces the following results:
welcome to print floyd's triangle in java please enter a number of rows : 7 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7
Program 2
import java.util.Scanner; //import scanner package public class Floyestrangles { public static void main(String[] args) { System.out.println("welcome to print Floyd's triangle in java"); //add a description System.out.println("please enter a number of rows :"); Scanner sc=new Scanner(System.in); //user input method int rows=sc.nextInt(); //get input from user to print Floyd's triangle for(int i=1; i<=rows; i++){ for(int j=1; j<=i; j++){ System.out.print(i +" ");//concatenate space in java //get small space among the numbers } System.out.println(); } } }
When the above code is executed, it produces the following results:
welcomr to print floyd's triangle in java please enter a number of rows : 7 1 22 333 4444 55555 666666 7777777
Program 3
package floydstriang; import java.util.Scanner; public class Floydstriang { public static void main(String[] args) { System.out.println("welcome to print Floyd's triangle in java"); System.out.println("please enter a number of rows :"); Scanner sc=new Scanner(System.in); int rows=sc.nextInt(); for(int i=10; i>=1; i--){ for(int j=1; j<=rows; j++){ System.out.print(j); } System.out.println(); rows=rows-1; } } }
When the above code is executed, it produces the following results:
welcome to print floyd's triangle in java please enter a number of rows : 8 12345678 1234567 123456 12345 1234 123 12 1
Program 4
import java.util.Scanner; class floydstriangle_3{ public static void main(String args[]){ int num=1,i,j,k;//Declare variable System.out.println("floyd's triangle number pattern: "); Scanner f3=new Scanner(System.in); System.out.println("Enter the number of row of you want: "); i=f3.nextInt(); //get input from user to print Floyd's triangle System.out.println("Here your Floyd's triangle "); for(j=1; j<=i; j++){ for(k=1; k<=j; k++){ System.out.print(num+" "); num++; } System.out.println(); } } }
When the above code is executed, it produces the following results:
Floyd's triangle number pattern: Enter the number of row of you want: 7 Here your floyd's triangle 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
Program 5
import java.util.Scanner; class floydstriangle_4{ public static void main(String args[]){ int num=1,i,j,k; //variable declaration System.out.println("Floyd's triangle number pattern: "); Scanner f3=new Scanner(System.in); //cretae a new scanner object System.out.println("Enter the number of row of you want: "); int row =f3.nextInt();//get the input from user System.out.println("Here your Floyd's triangle "); num=row*(row+1)/2; for(j=1; j<=row; j++){ for(k=1; k<=j; k++){ System.out.print(num+" "); num--; } System.out.println(); } } }
When the above code is executed, it produces the following results:
Floyd's triangle number pattern: Enter the number of row of you want: 7 Here your Floyd's triangle 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Program 6
//package Floyd's_triangle; import java.util.Scanner; //import scanner package public class Floyd's_trangle6 { public static void main(String args[]) { System.out.println("welcome to print Floyd's triangle in Java"); //add a description System.out.println("please enter the number of rows :"); Scanner sc=new Scanner(System.in); //user input metod int rows=sc.nextInt(); //get input from user to print Floyd's triangle for(int i=1; i<=rows; i++){ int k=i; for(int j=1; j<=rows; j++){ if(j<i) System.out.print(" "); else System.out.print(k++); } System.out.println(""); } } }
When the above code is executed, it produces the following results:
welcome to print Floyd's triangle in Java please enter the number of rows : 6 1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6
Suggested for you
If statements of Java language