C++ Pattern Program

1.Display the structure

*
* *
* * *
* * * *
* * * * *

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<=i; j++)
{
cout<<"* ";
}
cout<<"\n";
}
getch();
}
OUTPUT:
*
* *
* * *
* * * *
* * * * *

2.Display the structure

                   *
                 * *
               * * *
             * * * *
           * * * * *

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, j, k=8;
for(i=0; i<5; i++)
{
for(j=0; j<k; j++)
{
cout<<" ";
}
k=k-2;
for(j=0; j<=i; j++)
{
cout<<"* ";
}
cout<<"\n";
}
getch();
}
OUTPUT:
                *
              * *
            * * *
          * * * *
        * * * * *

3.Display the structure

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


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, j, n=1;
for(i=0; i<5; i++)
{
for(j=0; j<=i; j++)
{
cout<<n<<" ";
n++;
}
cout<<"\n";
}
getch();
}

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

4.Display the pattern

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, j, num=1;
for(i=0; i<5; i++)
{
num=1;
for(j=0; j<=i; j++)
{
cout<<num<<" ";
num++;
}
cout<<"\n";
}
getch();
}
OUTPUT:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

5.Display floyd’s triangle

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

#include<iostream.h>
int main()
{
int n,i,c,a=1;
cout<<"Enter the number of rows of Floyd's triangle to print\n";
cin>>n;
for(i=1;i<n;i++)
{
for(c=1;c<i;c++)
{
cout<<a;
a++;
}
cout<<"\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
Translate »