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
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
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
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
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
Suggested for you
For loop in C language
For loop in Python language
Nested for loop in Java language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…