Question

In: Computer Science

No Global variables No goto statement No break outside switch Write a menu driven C program...

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.

  1. Circle
  2. Triangle
  3. Exit

Based on the selected menu option, it asks the user for the following things -

  • Option 1 - asks for radius of a circle and then display its area in square units
  • Option 2 - asks for base and height of a triangle and then displays its area in square units.

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");
   }
   }

Solutions

Expert Solution

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..!!!


Related Solutions

Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End Program.The user can only input a positive integer (0 does not count) and if the input is not a positive integer the program must state "Invalid Input." The program must determine whether the input is even or odd. The program must use functions and can only use the iostream and iomanip libraries. Note: The program must loop until the 2nd menu option is chosen.
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
C++ Write a program using switch-case-break that will take an input number as for example 2...
C++ Write a program using switch-case-break that will take an input number as for example 2 and based on switch it will provide various powers of 2.
Write a menu-driven program to handle the flow of widgets into and out of a warehouse....
Write a menu-driven program to handle the flow of widgets into and out of a warehouse.     The warehouse will have numerous deliveries of new widgets and orders for widgets     The widgets in a filled order are billed at a profit of 50 percent over their cost     Each delivery of new widgets may have a different cost associated with it     The accountants for the firm have instituted a last-in, first-out system for filling orders         the newest...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program should allow the user to select one of the options according to whether lengths, weights or volume are to be converted, read the volue to be converted and the units, and then call the appropriate function to carry out the conversion In unit out unit I C (inch to centimeter 1 in = 2.4 cm) F C (feet to centimeter 1 ft = 30.4...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
A. Write a C++ with a menu (using switch) that asks the user to select one...
A. Write a C++ with a menu (using switch) that asks the user to select one of the following choices to the user: 1. Options ‘S’ or ‘s’ 2. Option ‘T’ or ‘t 3. Options ‘P’ or ‘p’ 4. print in the Matrix format B. When 1 is selected, prompt the user to enter the starting value st (int value). Use a single FOR loop to count numbers from 1 to st. When the loop is finished, find the average...
With C code Write a switch statement (not a complete program) which prints an appropriate message...
With C code Write a switch statement (not a complete program) which prints an appropriate message for a letter code entered. Use the following messages: If L is entered, output the message "Lakers" If C is entered, output the message "Clippers" If W is entered, output the message "Warriors" If any other character is entered, output the message "invalid code" Make sure to handle the case where the user enters in a small letter. That is, a capital or small...
In C++ Language English/Spanish Translation Program. Create a menu driven program that translates English to Spanish...
In C++ Language English/Spanish Translation Program. Create a menu driven program that translates English to Spanish and Spanish to English. Your translation program should use arrays for this program. You will need to populate the arrays with the contents of the English and Spanish data files provided. The two files are ordered such that each word in the one file corresponds to the respective translation in the other (i.e.: the first word in the ENG.txt file corresponds to the first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT