Floyd Triangle in java

Making Floyd Triangle


Steps to do this:-

1. take 4 integers n,i,j,a=1 ( n is for user input length i and j is for loop and a is for printing the pattern )

2.take the length from the user

3. start the i loop (i=1;i<=n;i++) and start the j loop inside the i loop (j=1;j<=i;j++) inside j loop print "a" and print " " ( blank string ) in next line then increment a (a++) 

close the j loop and and print a new line 
explanation :
1 st run it will print 1st row and 1st column 
2nd st run it will print 2nd row and 2nd column 

and so on according to user input length

4.Save and run

The Source Code

import java.util.Scanner;
class floyd
{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int n,i,j;// i and j is for loop and n is for user input length
        int a=1;
        System.out.println("enter the length");
        n=s.nextInt();
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=i;j++)
            {
                System.out.print(a);
                System.out.print(" ");// for printing a gap between two numbers
                a++;
            }
            System.out.println();
        }
    }
}

OUTPUT

Watch My Video 

Comments

Popular Posts