Table of Contents
In this tutorial, we will discuss Floyd’s triangle number pattern using while loop in Java.
We can print a lot of patterns (Floyd’s triangle or pyramid triangle)using nested while loop in java.
This example explains how to create Floyd triangle using java Nested while loop
How to print this number pattern using nested while loop in Java
123456
12345
1234
123
12
1
Program 1
When the above code is executed, the following output is displayed
123456
12345
1234
123
12
1
How to print this number pattern using nested while loop in Java
1
12
123
1234
12345
123456
Program 2
When the above code is executed, the following output is displayed
1
12
123
1234
12345
123456
Program 3
public class WhilePattern2{
public static void main (String args[]){
int i=1,j=1,k=1,n1=9,n2=10,count=0;
while(i<=n1){
k=1;
while(k<n1-(n1-i)){
System.out.print(" ");
k++;
}
count=n2-i;
j=1;
while(j<=count){
System.out.print(n1-(n1-j));
j++;
}
i++;
System.out.println(" ");
}
}
} When the above code is executed, it produces the following result
Program 4
import java.util.Scanner;
class Whilepatterns{
public static void main(String args[]){
System.out.println("floyd's triangle number pattren: ");
//ask input from the user
Scanner scan=new Scanner(System.in);
//cretae scanner object
System.out.println("Enter the number of row of you want: ");
int rows =scan.nextInt();//Takes the input from user
int row =1;
System.out.println("Here your floyd's triangle ");
int i=1;
while(i<=rows){//outer while loop
int j=1;
while(j<=i){//inner while loop
System.out.print(row+" ");
//print number with space
row++;
j++;
}
System.out.println();
//move to next line
i++;
}
}
} When the above code is executed, it produces the following result
While loop in Java While loop in C Language
Nested while loop in Java Nested while loop in C Language
Nested for loop in Java Nested for loop in C Language
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.