Q:

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

belongs to collection: Java Basic Solved Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

  • 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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java Program to Print Pascal Triangle usin... >>
<< Write a Java Program to print Armstrong numbers be...