Floyd's triangle

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

  1. write this program of your text editor
  2. save your file name like as class name with .java extension
  3. compile the program using Javac compiler
  4. 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

 

Suggested for you

For loop in C++ language

For loop in Java language

For loop in C language

For loop in Python language

Nested for loop in Java language

Nested for loop in C++ language

Nested for loop in C language

Nested for loop in Python language

Cpp program to Floyd's triangle star pattern
Cpp program to pyramid number pattern
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago