In: Computer Science
No Global variables
No goto statement
No break outside switch
Write a menu driven C program using functions and switch. Feel free to use “Empty Outlines” template from Canvas to design the functions as needed to build the code. Make sure to submit your work through Canvas. You can show me your code running in class when you are done.
The program shows following menu to the user repetitively until user selects option 3 to exit.
Based on the selected menu option, it asks the user for the following things -
You need to design functions such that each one accomplishes one task only. Please ask if you have any questions or need clarification about the specifications.
Area of circle = 3.14 * radius *radius
Area of triangle = 0.5 * base * height
#include <stdio.h>
#include <stdlib.h>
main() {
printf("Please select from the following menu
options:\n");
int choice, num, result, num1;
printf("Press 1 for circle\n");
printf("Press 2 for triangle\n");
printf("Press 3 to exit\n");
choice = input();
switch (choice) {
case 1: {
printf("Enter radius:\n");
num = input();
int result = 3.14 * num *
num;
printf("Area of sphere=");
output(result);
case 2: {
printf("Enter the base of the
triangle:\n");
num = input();
printf("Enter the height of the
triangle:\n");
num1 = input();
int result = 0.5 * num1 *
num;
printf("The area of the trangle
is:\n");
output(result);
}
case 3: {
printf("Thank you for calculating
with us\n");
}
default:
printf("wrong Input\n");
}
}
Dear Student,
Below i have write the complete C program as per the requirement.
===================================================================Program:
===================================================================
#include<stdio.h>
//function to calculate area of circle
double Cal_Circle_Area(double r)
{
double A = 3.14 * r;
return A;
}
//function to calculate the area of triangle
double Cal_Triangle_Area(double b, double h)
{
double A = 0.5 * b * h;
return A;
}
double get_radiues()
{
double r;
printf("Enter the radius of the circle: ");
scanf("%lf", &r);
return r;
}
//function to get the base
double get_base()
{
double b;
printf("Enter the base of the triange: ");
scanf("%lf", &b);
return b;
}
//function to get the height
double get_height()
{
double h;
printf("Enter the height of the triangle: ");
scanf("%lf", &h);
return h;
}
//start of main
int main()
{
//variables
double r, base, height, A;
int choice;
//display the menu
printf("Please select from the following menu
options:\n");
printf("Press 1 for circle\n");
printf("Press 2 for triangle\n");
printf("Press 3 to exit\n");
scanf("%d", &choice);
//switch the choice
switch(choice)
{
//case 1
case 1:
r = get_radiues();
A = Cal_Circle_Area(r);
printf("Area of circle is: %0.2f\n", A);
break;
//case 2
case 2:
base = get_base();
height = get_height();
A = Cal_Triangle_Area(base, height);
printf("Area of triangle is: %0.2f\n", A);
break;
//case 3
case 3:
printf("Thank you!\n");
break;
default:
printf("wrong input\n");
}
return 0;
}
//end of the main function
==================================================================
Sample Output:
=================================================================
Kindly Check and Verify Thanks..!!!