Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Java Program to print Floyd’s Triangle

by suresh

Write a Java Program to print Floyd’s Triangle with example. The Floyd’s Triangle is a right angled triangle with an array of natural numbers.

Java Program to print Floyd’s Triangle

This Java program allows the user to enter the maximum number of rows he/she want to print as Floyd’s triangle. Then, this program prints Floyd’s triangle of natural numbers until it reaches the user-specified rows.

// Java Program to print Floyd’s Triangle
package ShapePrograms;

import java.util.Scanner;

public class FloydTriangle {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int  rows, i,  j, number = 1;
		sc = new Scanner(System.in);		
		System.out.println(" Please Enter the Number of Rows you wish to see in FloydTriangle:  ");
		rows = sc.nextInt();
		System.out.println("---- Printing FLOYD'S Triangle ------");
		for ( i = 1 ; i <= rows; i++ ){
			for ( j = 1 ; j <= i; j++ ) {
				System.out.format("%d ", number);
				number++;
			}
			System.out.println("");
		}
	}
}
Java Program to print Floyd Triangle 1

The following statements will allow the User to enter the range or maximum Number of rows he/she want to print.

System.out.println(" Please Enter the Number of Rows you wish to see in FloydTriangle:  ");
rows = sc.nextInt();

Now let us see the Nested for loop in Java

for ( i = 1 ; i <= rows; i++ ){
	for ( j = 1 ; j <= i; j++ ) {
		System.out.format("%d ", number);
		number++;
	}
	System.out.println("");
}

Floyd’s Triangle Outer For Loop – First Iteration

From the above screenshot, please observe that the value of i is 1 and Rows is 5. So, the condition (i <= 5) is True, and it will enter into second for loop.

Inner For Loop – First Iteration

The j value is 1, and the condition (j <= 1) is True. So, it will start executing the statements inside the loop. The following statement will print Number as Output, and we all know that Number = 1. So, 1 printed.

System.out.format("%d ", number);

The below statement will increment the Value of Number by 1 using the Increment Operator

number++;

Inner For Loop – Second Iteration

The j value will be 2, and the condition (2 <= 1) is False so that it will exit from the second loop.

The following statement is to terminate the current line

System.out.println("");

Outer For Loop – Second Iteration

The value of i will be 2, and the condition (2 <= 5) is True. So, it will enter into second for loop.

Inner For Loop – First Iteration

The value of j is 1 ,and the condition (1 <= 2) is True. So, it will start executing the statements inside the loop. The following statement will print Number as Output, and we all know that Number = 2. So, it prints 2.

System.out.format("%d ", number);

The following statement will increment the Value of Number by 1

number++;

Next, j value will also be incremented by 1.

Inner For Loop – Second Iteration

The value of j is 2, and the condition (2 <= 2) is True. So, it will start executing the statements inside the loop. It means 3 printed.

Inner For Loop – Third Iteration

The j value will be 3, and the condition (3 <= 2) is False. So, it exits from the second loop.

It happens until it reaches 5, and after that, both the Inner Loop and Outer loop terminated.

Java Program to Print Floyd’s Triangle using Functions

This program allows the user to enter the maximum number of rows to print as Floyd’s triangle. Next, this program prints Floyd’s triangle of natural numbers using Functions.

package ShapePrograms;

import java.util.Scanner;

public class FloydTriangleusingMethods {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int  rows;
		sc = new Scanner(System.in);		
		System.out.println(" Please Enter the Number of Rows you wish to see in FloydTriangle:  ");
		rows = sc.nextInt();
		
		System.out.println("---- Printing FLOYD'S Triangle ------");
		FloydTriangle(rows);		
	}
	
	public static void FloydTriangle (int rows)  {
		int  i,  j, number = 1;
		for ( i = 1 ; i <= rows; i++ )  {
			for ( j = 1 ; j <= i; j++ )  {
				System.out.format("%d ", number);
				number++;
			}
			System.out.println("");
		}
	}
}
Java Program to print Floyd Triangle 2

When the compiler reaches to FloydTriangle(rows); line then the compiler will immediately jump to below function:

public static void FloydTriangle (int rows)  {

We already explained the LOGIC in the above example.

Java Program to Print Floyd’s Triangle without Natural numbers

This Java program allows the user to enter the number of rows he/she want to display as Floyd’s triangle. In this example, we are going to print Floyd’s triangle using the * symbol. This Java program can also say as printing Right angled triangle with * symbols.

// Floyd’s Triangle program in Java
package ShapePrograms;

import java.util.Scanner;

public class FloydTrianglewithoutNumbers {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int  rows, i,  j;
		sc = new Scanner(System.in);		
		System.out.println(" Please Enter the Number of Rows you wish to see in FloydTriangle:  ");
		rows = sc.nextInt();
		
		System.out.println("---- Printing FLOYD'S Triangle ------");
		for ( i = 1 ; i <= rows; i++ ) {
			for ( j = 1 ; j <= i; j++ ) {
				System.out.format("* ");
			}
			System.out.println("");
		}
	}
}
Java Program to print Floyd Triangle 3

Placed Under: Java Programs

  • Java Hello World Program
  • Java Add Two Numbers Program
  • Java Compound Interest Program
  • Java Number Divisible by 5 & 11
  • Java Cube of a Number Program
  • Java Print Even Numbers 1 to N
  • Java GCD of Two Numbers
  • Java LCM of Two Numbers
  • Java Largest of Two Numbers
  • Java Largest of Three Numbers
  • Java Print Multiplication Table
  • Java Odd Numbers from 1 to N
  • Java Odd Even Program
  • Java find +Ve or -Ve number
  • Java Power of a Number Program
  • Java Calculate Profit or Loss
  • Java Print 1 to 100 without Loop
  • Java Quadratic Equation roots
  • Java Square of Number Program
  • Java Simple Interest Program
  • Java Sum of Even Numbers
  • Java Sum of Odd numbers
  • Java Sum of Even & Odd Number
  • Java find Total ,Average & Percentage of 5 Subjects
  • Java ASCII Value of a Character
  • Java ASCII Value of all Characters
  • Java String Comparison program
  • Java Area of Circle Program
  • Java Area Of Triangle Program
  • Java Area Of Trapezoid Program
  • Java Area of Equilateral Triangle
  • Java Area of Rectangle Program
  • Java Area of Right angle triangle
  • Java Volume & Surface of Sphere
  • Java Volume & Surface of Cone
  • Java Volume & Surface of Cuboid
  • Java Volume & Surface of Cube
  • Java Cylinder Volume & Surface
  • Java Math Library
  • Java Armstrong Number Program
  • Java Perfect Number Program
  • Java Palindrome Program
  • Java Count Digits in a Number
  • Java Calculate Electricity Bill
  • Java Find Factors of a Number
  • Java Factorial Program
  • Java Fibonacci Series program
  • Java find First Digit of a Number
  • Java find Last Digit of a Number
  • Java First & Last Digit of Number
  • Java Generic Root of Number
  • Java Natural Numbers in Reverse
  • Java Natural Numbers from 1 to N
  • Java Find Sum of Prime Numbers
  • Java Print Prime Numbers 1 to N
  • Java Prime Number Program
  • Java Reverse a Number Program
  • Java Strong Number Program
  • Java Sum of N Natural Numbers
  • Java Sum of Digits Program
  • Java Swap Two Numbers
  • Java Leap Year Program
  • Java Number of Days in a Month
  • Java Array
  • Java Array Arithmetic Operations
  • Java Program to Copy an Array
  • Java Count Array Duplicates
  • Java count even number in array
  • Java count odd numbers in Array
  • Java Count Even & Odds in Array
  • Java Count Negative Array Items
  • Java Count Positive Array Items
  • Java Count Array +Ve & -Ve num
  • Java Delete Array Duplicates
  • Java Large & Small Array Item
  • Java Largest Array Number
  • Java Print Array Elements
  • Java Print Positive Array Items
  • Java Print Negative Array Items
  • Java Smallest Array Number
  • Java Second Largest Array Item
  • Java Print Unique Array Items
  • Java +Ve & -Ve in separate array
  • Java Sort Array in Descending
  • Java Sort Array in Ascending
  • Java Sum of Array Elements
  • Java Sum of Array Even Numbers
  • Java Sum of Array Odd Numbers
  • Java Sum of Array Even and Odd
  • Java Swap Arrays without temp
  • Java Matrix Introduction
  • Java Print Matrix items
  • Java Check 2 Matrices are Equal
  • Java Matrix Arithmetic operation
  • Java Matrix Addition
  • Java Matrix Multiplication
  • Java Find Matrix Determinant
  • Java Identity Matrix
  • Java Interchange Matrix diagonal
  • Java Matrix Lower Triangle
  • Java Matrix Lower Triangle Sum
  • Java Matrix Upper Triangle
  • Java Matrix Upper Triangle Sum
  • Java Scalar Matrix Multiplication
  • Java Sparse Matrix
  • Java Sum of Matrix Diagonal
  • Java Sum of Matrix Opp Diagonal
  • Java Sum of each Matrix Row
  • Java Sum of each Matrix Column
  • Java Sum of Matrix row & column
  • Java Symmetric Matrix
  • Java Transpose Matrix
  • Java Rectangle Star Pattern
  • Java Square Star Pattern
  • Java Hollow Box Number Pattern
  • Java Box Number Pattern of 1, 0
  • Java Square Number Pattern
  • Java Print Floyd’s Triangle
  • Java Print String Characters
  • Java String replace 1st Char Occ
  • Java String replace last Char occ
  • Java String remove 1st Char Occ
  • Java string remove last Char Occ
  • Java String remove 1st, last char
  • Java String remove all char occur
  • Java String Find 1st Char Occur
  • Java String find all char occur
  • Java String Find Last Char Occur
  • Java String Print 1st & last Char
  • Java String Chars ASCII values
  • Java Program to Concat Strings
  • Java String Characters count
  • Java Count Total Char Occur
  • Java Max Occur String Character
  • Java Toggle String Characters
  • Java Frequency of each Char
  • Java Count Vowels & Consonants
  • Java Count alph digits, special
  • Java Palindrome String
  • Java Program to Reverse a String
  • Java Min Occur String character
  • Java Convert Lower to Upper
  • Java Convert Upper to Lower
  • Java Count total String words

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy