Â
In this, we are going to see how to print the above pattern of right angle triangle of numbers/Floyd's Triangle, using nested for loops in C++.
The code given below can be used for TURBO C++ Compiler:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int rows, number = 1;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; i++)
{
for(int j = 1; j <= i; ++j)
{
cout << number << " ";
++number;
}
cout << endl;
}
getch();
}
The code given below can be used for g++/gcc Compiler:-
#include<iostream>
using namespace std;
int main()
{
int rows, number = 1;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; i++)
{
for(int j = 1; j <= i; ++j)
{
cout << number << " ";
++number;
}
cout << endl;
}
return 0;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP