Java code to triangle number pattern
- Home
- Floyd's triangle
- Java code to triangle number pattern
- On
- By
- 0 Comment
- Categories: Floyd's triangle, Number pattern
Java code to triangle number pattern
Java code to triangle number pattern
In this tutorial, we will learn about Java code to 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 with .java extension
- compile the program using Javac compiler
- finally, run the program (using .class file)
Program 1
Triangle number pattern program 1
import java.util.Scanner; public class Reverse_Pyramid{ public static void main(String args[]){ int rows; Scanner s=new Scanner(System.in); System.out.println("Enter number of rows"); rows=s.nextInt(); //get input from user for(int i=1; i<=rows; i++){ //parent for loop //to iterates row for(int j=1; j<=i; j++){ System.out.print(""); } for(int k=i; k<=rows; k++){ System.out.print(k+" ");//print number and space } System.out.println();//move to next line } } }
When the above code is executed, it produces the following results:
1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 3 4 5 6 7 8 4 5 6 7 8 5 6 7 8 6 7 8 7 8 8
Program 2
Triangle number pattern program 2
import java.util.Scanner; public class JavatrianglePattern2{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.print("Enter the number of rows: "); int rows=sc.nextInt(); for(int i=1; i<=rows; i++){//parent for loop lterate rows for(int j=1; j<=i; j++){ System.out.print(j+" ");//print number and space } for(int j=i-1; j>=1; j--){ System.out.print(j+" "); } System.out.println(); } System.out.println();//move to next line } }
When the above code is executed, it produces the following results:
1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 5 6 5 4 3 2 1 1 2 3 4 5 6 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
Program 3
Triangle number pattern program 3
import java.util.Scanner; public class JavatrianglePattern3{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.print("Enter the number of rows: "); int rows=sc.nextInt();//get input for rows from user for(int i=1; i<=rows; i++){//parent for loop lterate rows for(int j=i; j>=1; j--){ System.out.print(j+" ");//print number and space } System.out.println();//move to next line } } }
When the above code is executed, it produces the following results:
1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 6 5 4 3 2 1 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 1
Program 4
Triangle number pattern program 4
import java.util.Scanner; public class JavatrianglePattern4{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.print("Enter the number of rows: "); int rows=sc.nextInt();//get input for rows from user for(int i=1; i<=rows; i++){//parent for loop lterate rows int count=i; for(int j=1; j<=i; j++){ System.out.print(count+" ");//print number and space count=count+rows-j; } System.out.println();//move to next line } } }
When the above code is executed, it produces the following results:
1 2 10 3 11 18 4 12 19 25 5 13 20 26 31 6 14 21 27 32 36 7 15 22 28 33 37 40 8 16 23 29 34 38 41 43 9 17 24 30 35 39 42 44 45
Similar post
Floyd’s triangle number pattern using for loop in C
Floyd’s triangle pattern using nested for loop in Java
Floyd’s triangle pattern using nested while loop in Java
Hollow pyramid triangle pattern in C++ language
Suggested for you
Nested for loop in Java language
Nested for loop in C++ language
Nested for loop in Python language