Sunday 28 December 2014

Triangle Generator

Here is the program (in Dev-C++) which generates a triangle of size of your choice.

Specialty:
  •  Uses only two for loops.
  • Complexity is O(n^2)

#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
    int i,j,k;
    int n;
    cout<<"enter the no. of Rows :";
    cin>>n;
    k=n;

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=((2*n)-1);j++)
        {
            if(i+j >= n+1 && j<=k)
            cout<<"*";
            else
            cout<<" ";
        }
            k++;
            cout<<"\n";
    }
    getch();
    return 0;
}






No comments:

Post a Comment