Question

Write a program that displays a menu on the screen. The menu will give the user...

Write a program that displays a menu on the screen. The menu will give the user four options - enter two integers, enter two decimal numbers (#.#), enter one integer and one decimal number, or quit. The inputs should be labelled a, b, c, or d, and you should enter in the letter to choose the appropriate option. Once the user selects the option, two numbers will be read in from the user. The numbers will be added together and then the result will be displayed on the screen. In the case of adding two decimal numbers, make sure that the output shows only two digits after the decimal. For the case of adding an int and a float, give two results - the result where you treat the int as a float, and the result where you cast the float to an int.

write in C

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the solution:

#include <stdio.h>

int main(){
   char ch;
   int intNum1, intNum2; //declare integer value
   float floatNum1, floatNum2; //declare float value
   int intSum = 0; //declare integer to sum
   float floatSum = 0.0; //declare float to sum
   while(1){ //loop until user enter d to quit
       menu();//menu
       printf("\nEnter an option: ");
       scanf("%s",&ch);
       switch(ch){
           case 'a':
           case 'A':
               printf("Enter first number: "); //enter first number integer
               scanf("%d",&intNum1);
               printf("Enter second number: "); //enter first second integer
               scanf("%d",&intNum2);
               intSum = intNum1 + intNum2; //add the number
               printf("Sum of %d and %d is: %d",intNum1, intNum2, intSum); //display the integer result
               break;
           case 'b':
           case 'B':
               printf("\nEnter first number: "); //enter first number decimal
               scanf("%f",&floatNum1);
               printf("Enter second number: ");//enter second number decimal
               scanf("%f",&floatNum2);
               floatSum = floatNum1 + floatNum2; //add the number
               printf("Sum of %.2f and %.2f is: %.2f",floatNum1, floatNum2, floatSum); //display the decimal result
               break;  
           case 'c':
           case 'C':
               printf("\nEnter first integer number: "); //enter first number integer
               scanf("%d",&intNum1);
               printf("Enter second decimal number: "); //enter second number decimal
               scanf("%f",&floatNum1);
               floatSum = intNum1 + floatNum1; //add the number
               printf("Sum of %d and %.2f is: %.2f",intNum1, floatNum1, floatSum); //display the float result
               printf("\nSum of %d and %.2f is: %d",intNum1, floatNum1, (int)floatSum); //display the integer casting result
               break;  
           case 'd':
           case 'D':
               printf("Thank You!");
               exit(0);
               break;  
           default:
               printf("Error! invalid input.");
       }
   }
   return 0;
}

void menu(){
   printf("\n\na. Enter two integers\n");
   printf("b. Enter two decimal numbers (#.#)\n");
   printf("c. Enter one integer and one decimal number\n");
   printf("d. Quit\n");  
}

sample output:

a. Enter two integers
b. Enter two decimal numbers (#.#)
c. Enter one integer and one decimal number
d. Quit

Enter an option: a
Enter first number: 12
Enter second number: 13
Sum of 12 and 13 is: 25

a. Enter two integers
b. Enter two decimal numbers (#.#)
c. Enter one integer and one decimal number
d. Quit

Enter an option: b

Enter first number: 12.3
Enter second number: 13.123
Sum of 12.30 and 13.12 is: 25.42

a. Enter two integers
b. Enter two decimal numbers (#.#)
c. Enter one integer and one decimal number
d. Quit

Enter an option: c

Enter first integer number: 12
Enter second decimal number: 13.123
Sum of 12 and 13.12 is: 25.12
Sum of 12 and 13.12 is: 25

a. Enter two integers
b. Enter two decimal numbers (#.#)
c. Enter one integer and one decimal number
d. Quit

Enter an option: e
Error! invalid input.

a. Enter two integers
b. Enter two decimal numbers (#.#)
c. Enter one integer and one decimal number
d. Quit

Enter an option: d

Thank You!

Add a comment
Know the answer?
Add Answer to:
Write a program that displays a menu on the screen. The menu will give the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • In C++ write a simple menu program that displays a menu for the user in an...

    In C++ write a simple menu program that displays a menu for the user in an object-oriented technique. The menu should keep showing up until the user chooses to quit. Also, there is a sub-menu inside a menu. The user should get at least a line displayed after he or she chooses that particular option meaning if the user presses 1 for the read actors from the file. The output can look like "reading actors from a file" and then...

  • C++ Program with 2 functions 1. Hamming Functions Tester Write a function that displays a menu...

    C++ Program with 2 functions 1. Hamming Functions Tester Write a function that displays a menu to test the functions from 2 - 4. You must call the functions from 2 - 4 and cannot reimplement their functionality in this function. The menu is displayed as follows: 1) Enter a 4-bit message to encode into a 7-bit Hamming message. 2) Enter a 7-bit Hamming message to transmit through a noisy channel. 3) Enter a 7-bit Hamming message to receive and...

  • Write a C program that does the following: • Displays a menu (similar to what you...

    Write a C program that does the following: • Displays a menu (similar to what you see in a Bank ATM machine) that prompts the user to enter a single character S or D or Q and then prints a shape of Square, Diamond (with selected height and selected symbol), or Quits if user entered Q. Apart from these 2 other shapes, add a new shape of your choice for any related character. • Program then prompts the user to...

  • Write a C program that asks the user to enter two real numbers. Then your program...

    Write a C program that asks the user to enter two real numbers. Then your program displays a menu that asks the user to choose what arithmetic operation to be done on those numbers. Depending on the user's entry, the program should display the result to the screen. The sample runs below show what should be done to the numbers entered by the user. Your program should run exactly like shown in the sample runs. make your code run as...

  • IN JAVA: Write a program that displays a menu as shown in the sample run. You...

    IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...

  • Create a menu-driven program (using the switch) that finds and displays areas of 3 different objects....

    Create a menu-driven program (using the switch) that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1 -- rectangle 2 -- circle 3 -- triangle 4 -- quit If the user selects choice 1, the program should find the area of a rectangle. rectangle area = length * width If the user selects choice 2, the program should find the area of a circle. circle area = PI * radius * radius...

  • Write a Program that has a menu with a  layout below, that asks the user if they...

    Write a Program that has a menu with a  layout below, that asks the user if they want to: 1. Converts from feet and inches to meter and centimeters 2. Converts from meter and centimeters to feet and inches 3. Quit the program The program should continue as long as the user asks it to. The program will use a switch statement for the menu option selection. Either a do loo or a do-while loop can be used for the overall...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT