C++ Program – Display diamond and pascals triangle

by Nideesh C on May 2, 2011 · 0 comments

in C++




//Downloaded From theonlinetutorials.com
//(C)2011.All rights reserved.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,i,f,r,b,g,c,h,v=1,q,j,n,p,k=1;
int ch;
cout<<"Enter choice                1:STARS"<<endl;
cout<<"                            2:NUMBERS"<<endl;
cin>>ch;
switch(ch)
{
//WITH STAR
case 1:
v=2;
char a2[100][100];
cout<<"Enter Limit  : "<<endl;
cin>>n;
p=n-1;
//INPUTTING
for(i=0;i<=(2*n)-2;i++)
 {
 for(j=0;j<=(2*n)-2;j++)
 {
 a2[i][j]= '  ';
 }
 }
//STARTING
for(i=0;i<=(2*n)-2;i++)
 {
 for(j=0;j<=(2*n)-2;j++)
 {
 if(((i+j)==p)&&(i<n))
 {
 for(c=0;c<k;c++)
 {
 a2[i][j]='*';
 j=j+v;
 }
 k++;
 j=(2*n)-2;
 r=c-1;
 }
 if(((i+j)==(p+2))&&(i>=n))
 {
 for(h=r;h>0;h--)
 {
 a2[i][j]='*';
 j=j+v;
 }
 p=p+2;
 r--;
 j=(2*n)-2;
 }
 }
 }
for(i=0;i<=(2*n)-2;i++)
{
cout<<endl;
for(j=0;j<=(2*n)-2;j++)
{
cout<<a2[i][j];
}
}
break;

//CASE TWO WITH NUMBERS
case 2:
int a[100][100];
cout<<"Enter Limit   : "<<endl;
cin>>n;
p=n-1;
for(i=0;i<=(2*n)-2;i++)
{
for(j=0;j<=(2*n)-2;j++)
{
a[i][j]=0;
}
}

for(i=0;i<=(2*n)-2;i++)
{
for(j=0;j<=(2*n)-2;j++)
{
if(((i+j)==p)&&(i<n))
{
b=1;
for(c=0;c<k;c++)
{
if(j<=(n-1))
{
a[i][j]=b;
j=j+v;
b++;
r=b-2;
}
if(j>(n-1))
{
a[i][j]=r;
j=j+v;
r--;
}
}
k=k+2;
j=(2*n)-2;
r=c-2;
}
if(((i+j)==p+2)&&(i>=n))
{
f=1;
for(h=r;h>0;h--)
{
if(j<=(n-1))
{
a[i][j]=f;
j=j+v;
f++;
x=f-2;
}
if(j>(n-1))
{
a[i][j]=x;
j=j+v;
x--;
}
}
r=r-2;
p=p+2;
j=(2*n)-2;
}
}
}
for(i=0;i<=(2*n)-2;i++)
{
cout<<endl;
for(j=0;j<=(2*n)-2;j++)
{
if(a[i][j]!=0)
cout<<a[i][j];
else
cout<<" ";
}
}
break;

default:
cout<<"Wrong choice"<<endl;
break;

}
getch();

}



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ Program – Calculator
  2. C++ Program – Reversing each word in a string
  3. C++ Program – Convert decimal to binary & binary to decimal
  4. C++ Program – Convert Binary to Octal & Octal to Binary
  5. Sample – Program to print the first 10 lines of pascal’s triangle.

Leave a Comment

Previous post:

Next post: