1.
Introduction
2.
Floyd Triangle: Problem statement
2.1.
Input
2.2.
Output
2.3.
Explanation
2.4.
Approach
3.
C solution
3.1.
Code
3.2.
Output
4.
C++ solution
4.1.
Code
4.2.
Output
5.
Java Solution
5.1.
Code
5.2.
Output
6.
Python Solution
6.1.
Code
6.2.
Output
7.
Complexity
7.1.
Time Complexity
7.2.
Space Complexity
8.
FAQs
9.
Key Takeaways
Last Updated: Mar 27, 2024

# Floyd Triangle Number Pattern

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

Programmers are often encountered with a problem that is made up of mixing two or more concepts, and a good programmer knows how to approach the solution step by step. There is no need to come up with the most optimal solution first. We should first take time to build a general solution powered up by our first intuition.

To give your best in your interview at Amazon, Microsoft, or your other dream company, practice our 150 interview puzzles now, only on Coding Ninjas Studio.

Don't worry if you got stuck thinking about how to construct a Floyd triangle number pattern; remember, you either succeed or you learn. Today, we will help you learn about the Floyd Triangle pattern problem and discuss the approach to solving the problem in four different programming languages, i.e., CC++Java, and Python.

## Floyd Triangle: Problem statement

Given the 'n' number, representing the n-number of rows (starting with row number 1), we need to construct an n-row triangle such that the triangle elements follow a gradual increase in element by one, and the number of elements in a row is equal to the current row number.

For example:

### Input

``n = 4``

### Output

``````1  			// Row number 1
2 3 		// Row number 2
4 5 6 		// Row number 3
7 8 9 10 	// Row number 4``````

### Explanation

We start the series with 1 in the first row. We move to the following line because the number of elements in row 1 equals 1. Similarly, in row 2, when we get to the next two elements, we move to row number 3.

This pattern continues until the number of rows printed equals the user input of required rows.

### Approach

Let's discuss the approach to solve the Floyd triangle pattern program.

Step 1: We will define an initial variable count as 0.

Step 2: we will then take the input from the user, i.e., N.

Step 3: We will iterate through the 1st loop, N number of times starting from 1.

Step 4: We will nest another loop inside, which will loop from 1 to the outer loop variable.

Step 5: We will increase the count variable by one in the inner loop.

Step 6: We will print 'count' as the last line of the inner loop.

Step 7: We will print a new line character out of the inner loop.

Now, let us discuss the above approach's code to solve the Floyd triangle number pattern problem in different languages.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## C solution

Floyd triangle can be constructed in C programming language as given below:

### Code

``````int main()
{
int outer, inner, count = 0;
printf("Enter the number of rows: ");
int n_rows;
scanf("%d", &n_rows);
for (outer = 1; outer <= n_rows; outer++)
{
for (inner = 1; inner <= outer; inner++)
{
count++;
printf("%d ", count);
}
printf("\n");
}
return 0;
}``````

### Output

Enter the number of rows: 5

``````1
2 3
4 5 6
7 8 9 10
11 12 13 14 15``````

## C++ solution

Floyd triangle can be constructed in C++ programming language as given below:

### Code

``````#include <iostream>
using namespace std;
int main()
{
int outer, inner, count = 0;
cout<<"Enter the number of rows: ";
int n_rows;
cin>>n_rows;
for (outer = 1; outer <= n_rows; outer++)
{
for (inner = 1; inner <= outer; inner++)
{
count++;
cout<<count<<" ";
}
cout<<endl;
}
return 0;
}``````

### Output

Enter the number of rows: 4

``````1
2 3
4 5 6
7 8 9 10 ``````

If you are starting out with C++, our guided path for C++ will help you get a head start over others!

## Java Solution

Floyd triangle can be constructed in Java programming language as given below:

### Code

``````import java.util.Scanner;
public class FloydTriangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int outer, inner, count = 0;
int n_rows = sc.nextInt();
for (outer = 1; outer <= n_rows; outer++) {
for (inner = 1; inner <= outer; inner++) {
count++;
System.out.print(count + " ");
}
System.out.println();
}
sc.close();
}
}``````

### 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 ``````

If you are starting out with Java, our guided path for Java will help you get a head start over others!

## Python Solution

Floyd triangle can be constructed in Python programming language as given below:

### Code

``````n_rows = int(input("Enter the number of rows: "))
count = 0

for outer in range(1, n_rows+1):
for inner in range(1, outer+1):
count+=1
print(count, end=" ")
print("\r")``````

### Output

Enter the number of rows: 4

``````1
2 3
4 5 6
7 8 9 10 ``````

If you are starting out with python, our guided path for python will help you get a head start over others!

## Complexity

Let's discuss the time and space complexity of the above approach.

### Time Complexity

Since the logic and approach is the same, the time complexity of the solution in all the languages remains the same, i.e., O(n2) for using two nested loops ranging from 1 to n, where n is the number of rows.

### Space Complexity

Since the logic and approach is the same, and no extra space is used, the space complexity of the solution in all the languages also remains the same, i.e., O(1).

## FAQs

1. What are pattern problems?
Pattern programs are simply patterns of integers, alphabets, or symbols in a specific order. The for loop condition may be used to solve these pattern programs quickly.

2. What is the difference between the Floyd triangle and the Fibonacci half triangle?
The Floyd triangle flows a simple AP series with a common difference of 1 starting from the first to the last triangle element. In contrast, the Fibonacci half triangle is an arrangement of the Fibonacci series in the form of a triangle.

3. Why do we need to practice pattern problems?
The easiest way to learn how looping structures in general-purpose programming languages function is to use pattern programs. It aids newcomers in visualizing how each loop iteration works. This question is frequently asked during campus placements and employment interviews.

## Key Takeaways

This article extensively discussed the famous pattern problem of constructing the Floyd triangle in C, C++, Java, and Python with illustrative examples. We want you to solve these types of problems or even more difficult ones next time, for which we have curated a unique path for you to take your problem-solving skills to the next level.

We hope this blog has helped you enhance your knowledge regarding Floyd Triangle Number Pattern, and if you would like to learn more, check out our interview puzzle listDSA problems or go through our test series. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass