Print Floyd's Triangle Pattern in C / C++ / Java / Python / C#

Floyd's Triangle pattern

Programs to print the Floyd's triangle pattern have been given here. The number of rows of the pattern is taken as input.






1. Program & output to print the floyd's triangle pattern




1.1. C Program & output to print the floyd's triangle pattern

Code has been copied
/***************************************
	  alphabetacoder.com
C program to print the Floyd's triangle
****************************************/

#include <stdio.h>

int main() {
    // declare variables
    int row, i, j, num = 1;

    // take input
    printf("Enter the number of rows: ");
    scanf("%d", & row);

    // new line
    printf("\n");

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print numbers
        for (j = 1; j <= i; j++) {
            printf("%d\t", num);

            // increment num
            num++;
        }
        // new line
        printf("\n");
    }

    return 0;
}

Output


Enter the number of rows: 6


1

2    3

4    5    6

7    8    9    10

11   12   13   14   15

16   17   18   19   20   21




1.2. C++ Program & output to print the floyd's triangle pattern

Code has been copied
/****************************************
	  alphabetacoder.com
C++ program to print the Floyd's triangle
*****************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j, num = 1;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print numbers
        for (j = 1; j <= i; j++) {
            cout << num << "\t";

            // increment num
            num++;
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 2


1

2    3




1.3. Java Program & output to print the floyd's triangle pattern

Code has been copied
/******************************************
	    alphabetacoder.com
Java program to print the Floyd's triangle
*******************************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);
        // declare variables
        int row, i, j, num = 1;

        // take input 
        System.out.print("Enter the number of rows: ");
        row = sc.nextInt();

        // new line
        System.out.println("");

        // display the pattern
        for (i = 1; i <= row; i++) {
            // print numbers
            for (j = 1; j <= i; j++) {
                System.out.print(num + "\t");

                // increment num
                num++;
            }
            // new line
            System.out.println("");
        }
    }
}

Output


Enter the number of rows: 5


1

2    3

4    5    6

7    8    9    10

11   12   13   14   15




1.4. Python Program & output to print the floyd's triangle pattern

Code has been copied
#*********************************************
#           alphabetacoder.com
#Python program to print the Floyd's triangle
#*********************************************

# take input
row = int(input("Enter the number of rows: "))

# initialize
num = 1

# new line
print("")

# display the pattern
for i in range(1, row + 1):
    # print numbers
    for j in range(1, i + 1):
        print(num, end = "\t")
        # incement num
        num = num + 1
    # new line
    print("")

Output


Enter the number of rows: 4


1

2    3

4    5    6

7    8    9    10




1.5. C# Program & output to print the floyd's triangle pattern

Code has been copied
/***************************************
	    alphabetacoder.com
C# program to print the Floyd's triangle
****************************************/

using System;

namespace FloydTriangle {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int row, i, j, num = 1;

            // take input
            Console.Write("Enter the number of rows: ");
            row = Convert.ToInt32(Console.ReadLine());

            // new line
            Console.WriteLine("");

            // display the pattern
            for (i = 1; i <= row; i++) {
                // print numbers
                for (j = 1; j <= i; j++) {
                    Console.Write(num + "\t");

                    // increment num
                    num++;
                }
                // new line
                Console.WriteLine("");
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the number of rows: 3


1

2    3

4    5    6