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

 

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

Pyramid star pattern in Cpp

Pyramid star pattern in C

Pyramid star pattern in Java 

 

Suggested for you

Loops in Java language

Loops in C language

Loops in C++ language

 

For loop in C++ language

For loop in Java language

For loop in C language

For loop in Python language

 

While loop in Java language

While loop in C language

While loop in C++ language

While 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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago