Home / C / c program to print the desired shape empty or filled

c program to print the desired shape empty or filled

First parameter should be a character to choose the shape that will be printed.
It could be ‘T’ or ‘t’ for triangle,
‘R’ or ‘r’ for rectangle,
‘P’ or ‘p’ for parallelogram,
‘D’ or ‘d’ for diamond.
Second parameter should be a number which identifies the height of the shape.
The height should be minimum 2 for triangle, rectangle and parallelogram.
The height should be minimum 3 and an odd number for diamond.

Third parameter should be a number which identifies the width of the shape.
The width should be minimum 2 for rectangle and parallelogram.
The width should be assumed equal to the height for diamond and triangle. Therefore
if the selected shape is diamond or triangle, width don’t have to be asked to user.

Forth parameter should be a character which identifies the emptiness of the shape.
It could be ‘E’ or ‘e’ for empty shape,
‘F’ or ‘f’ for full shape.
Fifth parameter should be character that will be used for printing shape.

#include<stdio.h>
#include<conio.h>
void main()
{
int flag=0,i,j,k,height=0,width=0;
char ch,empty,fill;
printf("---------Press 'E' or 'e' to Exit----------");
while(ch!='e' || ch!='E')
{

do
{
	printf("\n\n -----------PRESS E TO EXIT--------- \n FIRST PARAMETER: CHOOSE THE SHAPE : \n 'T' or 't' for TRIANGLE \n 'R' or 'r' for rectangle \n 'P' or 'p' for parallelogram \n 'd' or 'D' for diamond ENTER YOUR CHOICE\n");
	ch=getch();

switch(ch)
{
case 't':
case 'T': flag=1;
	  printf("\n\n SECOND PERIMETER : enter the height of a trianlge (minimum=3)  :");
	  scanf("%d",&height);
	  printf("\nFOURTH PERMIMETER : enter 'E' for empty and 'F' for filled figure  :");
	  empty=getch();
	  printf("\nFIFTH PERIMETER :enter the character for printing the shape  :");
	  fill=getch();
	for(i=1;i<=height;i++)
	{       printf("\n");
		for(j=1;j<=i;j++)
		{
			if(j%i==0 | j%i==1 | i==height)
				printf("%c",fill);
			else if(empty=='e' | empty=='E')
				printf(" ");
			else
				printf("%c",fill);
		}

	}

break;
case 'r':
case 'R': flag=1;
	  printf("\n\n SECOND PREIMETER : enter the height for rectangle (minimum=2) :");
	  scanf("%d",&height);
	  printf("\nTHIRD PERIMETER: enter the width of a rectangle (minimum=2) :");
	  scanf("%d",&width);
	  printf("\nFOURTH PERIMETER : enter 'E' for empty and 'F' for filled figure :");
	 empty=getch();
	  printf("\nFIFTH PERIMETER: enter the character for printing the shape : ");
	  fill=getch();
	  for(i=1;i<=height;i++)
	  {    printf("\n");
		for(j=0;j<width;j++)
		{
			if(j%width==0 |j%(width-1)==0 | i%height==0 | i%height==1 )
				printf("%c",fill);
			else if(empty=='e' | empty=='E')
				printf(" ");
			else
				printf("%c",fill);
		}

	  }
break;

case 'p':
case 'P': flag=1;
	  printf("\n\nSECOND PERIMETER :enter the height for parallelogram (minimum=2) :");
	  scanf("%d",&height);
	  printf("\n THIRD PERIMETER : enter the width of a parallelogram (minimum=2) :");
	  scanf("%d",&width);
	  printf("\n FOURTH PERIMETER : enter 'E' for empty and 'F' for filled figure :");
	  empty=getch();
	  printf("\n FIFTH PERIMETER : enter the character for printing the shape :");
	  fill=getch();
	  for(i=1;i<=height;i++)
	  {     printf("\n");
		for(k=0;k<i;k++)
		printf(" ");
		for(j=0;j<width;j++)
		{
			if(j%height==0 | i%height==0 | i%height==1 )
				printf("%c",fill);
			else if(empty=='e' | empty=='E')
				printf(" ");
			else
				printf("%c",fill);
		}

	  }
break;
case 'd':
case 'D': flag=1;
	  printf("\n\Nenter the height for Diamond (odd number)");
	  scanf("%d",&height);
	  printf("\nenter 'E' for empty and 'F' for filled figure");
	  empty=getch();
	  printf("\nenter the character for printing the shape");
	  fill=getch();
	  for(i=1;i<=height/2;i++)
	  {                  printf("\n");
		for(j=i;j<=height/2;j++)
		printf(" ");
		for(j=1;j<=2*i-1;j++)
		{
			if(j==1 | j==2*i-1 | empty=='f' | empty=='F')
			printf("%c",fill);
			else
			printf(" ");
		}
	  }
	  for(i=height/2+1;i>0;i--)
	  { printf("\n");
	  for(j=height/2+1;j>i;j--)
	  printf(" ");
	  for(j=1;j<=i*2-1;j++)
	  {
		if(j==1 | j==2*i-1 | empty=='f' | empty=='F')
		printf("%c",fill);
		else
		printf(" ");
	  }
	  }

break;
case 'e':
case 'E': exit(0);
break;
default: printf("------wrong choice enter again-------");
}
}while(flag!=1);
	}
	}

Check Also

Restrict Mouse Pointer Position C Graphics Program

This C Program will restrict the Mouse Pointer location or to a particular position or x,y ...

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.