Write a Java Program to Print Floyd’s Triangle using For Loop

By | 29.01.2017

Write a Java Program to Print Floyd’s Triangle using For Loop


 

To print the Floyd’s triangle in Java programming, you have to use two for loops, the outer loop is responsible for rows and the inner loop is responsible for columns and start printing the Floyd’s triangle as shown in the following program.

As we know that, Floyd’s triangle is a right angled-triangle using the natural numbers, so the following Java Program ask to the user to enter the range ( how many line he/she want) to print the Floyd’s Triangle.

  • Following is the sample of Floyd’s Triangle :

 

SOURCE CODE ::

 

import java.util.Scanner;
public class FloydTriangle {

    public static void main(String[] args) {
        
        int i,j,k=1,n;
        System.out.print("Enter how many lines u want : ");
        Scanner sc = new Scanner(System.in);
        n=sc.nextInt();
        for(i=1;i<=n;i++)
        {
           
            for(j=1;j<=i;j++)
            {
                
                System.out.print(" "+k);
                k++;
            }
            System.out.println("");
        }
    }
    
}

 

OUTPUT ::

 

Enter how many lines u want : 6
 1
 2 3
 4 5 6
 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20 21

 

5 2 votes
Article Rating
Category: Basic Programs Java Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments