C++ program to print a diamond inside a box using asterisks.
output
You may also like:
C++ program to print a diamond in a box - 1
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,x,n=1,t=1,l;
clrscr();
cout<<"Enter limit\n";
cin>>l;
x=l/2;
for(i=0;i<l;i++)
{
for(j=0;j<l;j++)
{
if(i>0&&i<(l-1))
{
if(j>=x&&n>0)
{
cout<<"*";
n-=1;
}
else if(i==0||i==(l-1)||j==0||j==(l-1))
{
cout<<"*";
}
else
{
cout<<" ";
}
}
else
{
if(i==0||i==(l-1))
{
cout<<"*";
}
}
}
if(i>0&&i<(l/2))
{
t=t+2;
n=t;
x-=1;
}
else
{
t=t-2;
n=t;
x+=1;
}
cout<<endl;
}
getch();
}
#include<conio.h>
void main()
{
int i,j,x,n=1,t=1,l;
clrscr();
cout<<"Enter limit\n";
cin>>l;
x=l/2;
for(i=0;i<l;i++)
{
for(j=0;j<l;j++)
{
if(i>0&&i<(l-1))
{
if(j>=x&&n>0)
{
cout<<"*";
n-=1;
}
else if(i==0||i==(l-1)||j==0||j==(l-1))
{
cout<<"*";
}
else
{
cout<<" ";
}
}
else
{
if(i==0||i==(l-1))
{
cout<<"*";
}
}
}
if(i>0&&i<(l/2))
{
t=t+2;
n=t;
x-=1;
}
else
{
t=t-2;
n=t;
x+=1;
}
cout<<endl;
}
getch();
}
output
You may also like:
C++ program to print a diamond in a box - 1
No comments:
Post a Comment