Friday, February 13, 2015

C Program to print a triangle or square of s according to user choice


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k,n,ch;
cout<<"What you want to Print

"<<"1.Triangle
"<<"2.Square
"<<"Enter your choice: ";
cin>>ch;
cout<<"
How much long....?(ex:8): ";
cin>>n;
switch(ch)
{
case 1:for(i=1;i<=n;++i)
{
for(j=n-1;j>=i;--j)
cout<<" ";
for(k=1;k<=(i*2-1);++k)
{
if(i==n)
cout<<"*";
else
{ if(k==1||k==(i*2-1))
cout<<"*";
else
cout<<" ";
}
}
cout<<"
";
}
break;
case 2: for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
if(i==1||i==n)
cout<<"*";
else
{ if(j==1||j==n)
cout<<"*";
else
cout<<" ";
}
}
cout<<"
";
}
break;
}
getch();
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.