# Python Program to Print Floyd’s Triangle

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Given the number of rows of the triangle, the task is to print Floyd’s triangle in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the triangle = 10

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55

Example2:

Input:

Given number of rows of the triangle = 13

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91

## Program to Print Floyd’s Triangle in C, C++, and Python

Below are the ways to print Floyd’s triangle in C, C++, and Python.

### Method #1: Using For Loop (Static Input)

Approach:

• Give the number of rows of the triangle as static input and store it in a variable.
• Take a variable and initialize it with 1 say sampNum.
• Loop from 1 to the number of rows of the triangle using For loop.
• Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
• Inside the inner for loop print the sampNum with a space character.
• Increase the value of sampNum by 1.
• Print the Newline Character after the end of the inner for loop.
• The Exit of the program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the triangle as static input and store it in a variable.
triRows = 10
# Take a variable and initialize it with 1 say sampNum.
sampNum = 1
# Loop from 1 to the number of rows of the triangle using For loop.
for m in range(1, triRows+1):
# Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
for n in range(1, m+1):
# Inside the inner for loop print the sampNum with a space character.
print(sampNum, end=' ')
# Increase the value of sampNum by 1.
sampNum = sampNum+1
# Print the Newline Character after the end of the inner for loop.
print()


Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

// Give the number of rows of the triangle as static
// input and store it in a variable.
int triRows = 10;
// Take a variable and initialize it with 1 say sampNum.
int sampNum = 1;
// Loop from 1 to the number of rows of the triangle
// using For loop.
for (int m = 1; m <= triRows; m++) {
for (int n = 1; n <= m; n++) {
// Inside the inner for loop print the
// sampNum with a space character.
cout << sampNum << " ";
// Increase the value of sampNum by 1.
sampNum = sampNum + 1;
}

// Print the Newline Character after the end of the
// inner for loop.
cout << endl;
}
return 0;
}

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

// Give the number of rows of the triangle as static
// input and store it in a variable.
int triRows = 10;
// Take a variable and initialize it with 1 say sampNum.
int sampNum = 1;
// Loop from 1 to the number of rows of the triangle
// using For loop.
for (int m = 1; m <= triRows; m++) {
for (int n = 1; n <= m; n++) {
// Inside the inner for loop print the
// sampNum with a space character.
printf("%d ", sampNum);
// Increase the value of sampNum by 1.
sampNum = sampNum + 1;
}

// Print the Newline Character after the end of the
// inner for loop.
printf("\n");
}
return 0;
}

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55

### Method #2: Using For Loop (User Input)

Approach:

• Give the number of rows of the triangle as user input and store it in a variable.
• Take a variable and initialize it with 1 say sampNum.
• Loop from 1 to the number of rows of the triangle using For loop.
• Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
• Inside the inner for loop print the sampNum with a space character.
• Increase the value of sampNum by 1.
• Print the Newline Character after the end of the inner for loop.
• The Exit of the program.

1) Python Implementation

Give the number of rows of the triangle as user input using the int(input()) function and store it in a variable.

Below is the implementation:

# Give the number of rows of the triangle as user input
# using the int(input()) function and store it in a variable.
triRows = int(input('Enter some random number of rows of the triangle = '))
# Take a variable and initialize it with 1 say sampNum.
sampNum = 1
# Loop from 1 to the number of rows of the triangle using For loop.
for m in range(1, triRows+1):
# Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
for n in range(1, m+1):
# Inside the inner for loop print the sampNum with a space character.
print(sampNum, end=' ')
# Increase the value of sampNum by 1.
sampNum = sampNum+1
# Print the Newline Character after the end of the inner for loop.
print()


Output:

Enter some random number of rows of the triangle = 11
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66



2) C++ Implementation

Give the number of rows of the triangle as user input using the cin function and store it in a variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

// Give the number of rows of the triangle as user input
// using the cin function and store it in a variable.
int triRows;
cout<<"Enter some random number of rows of the triangle = ";
cin >> triRows;

// Take a variable and initialize it with 1 say sampNum.
int sampNum = 1;
// Loop from 1 to the number of rows of the triangle
// using For loop.
for (int m = 1; m <= triRows; m++) {
for (int n = 1; n <= m; n++) {
// Inside the inner for loop print the
// sampNum with a space character.
cout << sampNum << " ";
// Increase the value of sampNum by 1.
sampNum = sampNum + 1;
}

// Print the Newline Character after the end of the
// inner for loop.
cout << endl;
}
return 0;
}

Output:

Enter some random number of rows of the triangle = 13
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91



3) C Implementation

Give the number of rows of the triangle as user input using the scanf function and store it in a variable.

Below is the implementation:

#include <stdio.h>

int main()
{

// Give the number of rows of the triangle as user input
// using the scanf function and store it in a variable.
int triRows;
scanf("%d", &triRows);
// Take a variable and initialize it with 1 say sampNum.
int sampNum = 1;
// Loop from 1 to the number of rows of the triangle
// using For loop.
for (int m = 1; m <= triRows; m++) {
for (int n = 1; n <= m; n++) {
// Inside the inner for loop print the
// sampNum with a space character.
printf("%d ", sampNum);
// Increase the value of sampNum by 1.
sampNum = sampNum + 1;
}

// Print the Newline Character after the end of the
// inner for loop.
printf("\n");
}
return 0;
}

Output:

number of rows =4
1
2 3
4 5 6
7 8 9 10

Related Programs: