Write a program in C which is a Menu-Driven Program to compute the area of the various geometrical shapes.

Previous
Next

Approach-1:

  • Simple menu driven programs are very helpful to students in application development.
  • In this code, we created a simple menu used to perform all computational operations on geometrical shapes
  • In this approach, we execute the code only once with choice.
#include <stdio.h>
int main()
{
	int ch,r,l,w,b,h;
	float area;
	printf("Area of Circle\n");
	printf("Area of Rectangle\n");
	printf("Area of Triangle\n");
	printf("Enter your choice : ");
	scanf("%d",&ch);
	switch(ch)
	{
		case 1:	printf("Input radious of the circle : ");
     				scanf("%d",&r);
      				area=3.14*r*r;
       				break;
		case 2:	printf("Input length & width : ");
				scanf("%d%d",&l,&w);
  				area=l*w;
    				break;
  		case 3:	printf("Input the base and height triangle :");
   				scanf("%d%d",&b,&h);
   				area=.5*b*h;
     				break;
 	}
 	printf("The area is : %f\n",area);
 	return 0;
}

Approach-2:

  • In this approach, we execute the logic with Iterators(while loop) until user enter the choice “quit”.
  • Student need not to run the code every time to calculate the area of shapes. In a single run, we can repeat the code as many as times required.
#include <stdio.h>
void circle();
void rectangle();
void triangle();
int ch,r,l,w,b,h;
int main()
{
	while(1)
	{
		printf("1. Area of Circle\n");
		printf("2. Area of Rectangle\n");
		printf("3. Area of Triangle\n");
		printf("4. Quit\n");
		printf("Enter your choice : ");
		scanf("%d",&ch);
		switch(ch)
		{
			case 1:	circle();
					break;
			case 2:	rectangle();
					break;
    			case 3:	triangle();
					break;
			case 4:	exit(1);
			default:	printf("Invalid choice\n\n");
	 	}
	}
return 0;
}
void circle(){
	float area;
	printf("Input radious of the circle : ");
	scanf("%d",&r);
	area=3.14*r*r;
	printf("The area is : %f\n",area);
}
void rectangle(){
	float area;
	printf("Input length & width : ");
	scanf("%d%d",&l,&w);
	area=l*w;
	printf("The area is : %f\n",area);
}
void triangle(){
	float area;
	printf("Input the base and hight :");
	scanf("%d%d",&b,&h);
	area=.5*b*h;
	printf("The area is : %f\n",area);
}
Previous
Next

Courses Enquiry Form