19 March, 2012

C++ Program For Printing A Floyd's Triangle.

This Post Contains A C++ Program For Printing A Floyd's Triangle With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop', 'If...else' & 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:

1
2   3
4   5   6
7   8   9   10

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program For Printing A Floyd's Triangle.

/* Declaration Of Header Files */
# include <iostream.h>
# include <conio.h>

/* Start Of Main Program */
void main()
{
   /* Declaration Of Variables */
   int a, b, c, rows;
   clrscr();
 
   /* Asking For The Input From User */ 
   cout << " Enter No. Of Rows To Print : ";
   cin >> rows;
 
   /* Source Code For Computing Floyd's Triangle */
   b  =  1;
   for ( a = 0; a < rows; a++ )
   {
       for ( c = 0; c <= a; c++ )
       {
           if (  b  <  10  )
              cout << b << "  ";
           else
              cout << b<< "  ";
              b++;
       }
       cout << " \n  \n ";
   }
   getch();
}
/* End Of Main Program */ 

Output :

Enter No. Of Rows : 4
1
2   3
4   5   6
7   8   9   10




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.