Question

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 users 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 shown in the sample runs below.

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

Please find the code below::

#include <stdio.h>

#include <math.h>

void printOperations(){

printf("Enter \n");

printf(" 1\tto print the sum of the numbers\n");

printf(" 2\tto print the product/multiplication of the numbers\n");

printf(" 3\tto print the division of first number by second number\n");

printf(" 4\tto print the square root of the first number\n");

printf(" 5\tto Exit program\n");

printf("1,2,3 or 4 : ");

}

int main()

{

float n1,n2;

printf("Enter your first number : ");

scanf("%f",&n1);

printf("Enter your second number : ");

scanf("%f",&n2);

int choice;

float sum,mul,root,div;

while(1){

printOperations();

scanf("%d",&choice);

switch (choice) {

case 5 :

printf("Good bye!");

break;

case 1 :

sum = n1+n2;

printf("%.2f + %.2f = %.2f",n1,n2,sum);

break;

case 2 :

mul = n1*n2;

printf("%.2f * %.2f = %.2f",n1,n2,mul); break;

case 3 :

if(n2==0){

printf("Sorry you cannot divide by zero!\n");

continue;

}

div = n1/n2;

printf("%.2f / %.2f = %.2f",n1,n2,div);

break;

case 4 :

if(n1<0){

printf("Sorry we cannot square root a negative number!\n");

continue;

}

root = sqrt(n1);

printf("Square root of %.2f is %.2f",n1,root);

break;

default:

printf("Invalid option!!!");

break;

}

if(choice==5){

break;

}

printf("\n");

}

return 0;

}

output:

CAUsers Mohammad Shahrukh\Desktoplc_language_workspace,C\Ul.exe Enter your first number -2.4 Enter your second number: 2 Enter 1 to print the sum of the numbers 2 to print the product/multiplication of the numbers 3 to print the division of first number by second number 4 to print the square root of the first number 5 to Exit program 1,2,3 or 4:1 2.402.000.40 Enter 1 to print the sum of the numbers 2 to print the product/multiplication of the numbers 3 to print the division of first number by second number 4 to print the square root of the first number 5 to Exit program 1,2,3 or 4:2 2.40 2.00 -4.80 Enter 1 to print the sum of the numbers 2 to print the product/multiplication of the numbers 3 to print the division of first number by second number 4 to print the square root of the first number 5 to Exit program 1,2,3 or 4:3 2.40 2.00-1.20 Enter 1 to print the sum of the numbers 2 to print the product/multiplication of the numbers 3 to print the division of first number by second number 4 to print the square root of the first number 5 to Exit program 1,2,3 or 4:4 Sorry we cannot square root a negative number! Enter 1 to print the sum of the numbers 2 to print the product/multiplication of the numbers 3 to print the division of first number by second number 4 to print the square root of the first number 5 to Exit program 1,2,3 or 4:

Add a comment
Know the answer?
Add Answer to:
Write a C program that asks the user to enter two real numbers. Then your program...
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
  • Write an assembler program that asks the user (as shown below) for two integers and a...

    Write an assembler program that asks the user (as shown below) for two integers and a single character representing the arithmetic operations: addition, subtraction, multiplication and integer division (displays both quotient and remainder). Perform the requested operation on the operands and display the result as specified below. Assume SIGNED NUMBERS. The program should not divide by 0! If the user attempts to divide by 0, display the error message: "Cannot divide by zero." If this error occurs, the program should...

  • write a program code that asks the user how many numbers they wish to find the...

    write a program code that asks the user how many numbers they wish to find the statistics of and then asks the user to enter those numbers. The program must then calculate and display the average variance and standard deviation of the numbers entered by the user. the program code must -ask three user how many numbers they wish to find the statistics of -ask the user to enter those numbers and store them in an array - use a...

  • Write a Java program which allows the user to perform simple tasks on a calculator. A...

    Write a Java program which allows the user to perform simple tasks on a calculator. A series of methods allows the user to select an operation to perform and then enter operands. The first method displays a menu, giving the user the choice of typing in any one of the following: +, -, *, /, or % representing the usual arithmetic operators (Each operation should be done on numbers negative(-) to positive(+), positive(+) to negative(-), negative(-) to negative(-), and positive(+)...

  • Write a Java program that: • Asks the user to enter the number of integers that...

    Write a Java program that: • Asks the user to enter the number of integers that he need to enter; • Asked him to enter integer by integer; • Print The number of Odd numbers entered and the number of Even numbers entered. Important notes: 1. You should have to copy and paste the Java as your answer for this question. DON’T take screen shot for your Java Code. It must be editable. 2. Take a screen shot for your...

  • Write a program which asks the user to enter an integer. Use switch statement to write...

    Write a program which asks the user to enter an integer. Use switch statement to write out the numeric word (such as ONE) if the user's input is 1; (see the sample run output for the usr input 2 or 3); otherwise, write OUT OF RANGE. Below are few sample runs: If the user enters a 1, the program will print: ONE TWO THREE Or, if the user enters a 2, the program will print: TWO THREE Or, if the...

  • Write a program that asks the user to enter number, and displays all the numbers that...

    Write a program that asks the user to enter number, and displays all the numbers that are multiples of 2 and 5 smaller than or equal to the number entered by the user. Hint: A number n is a multiple of 2 if the remainder of the division of n by 2 is equal to zero. Your program should have an output similar to the following: Please enter a number: 50 The multiples of 2 and 5 less than or...

  • RandomGame.cpp (2pt) Write a program that asks the user to enter his/her name. Then begin a...

    RandomGame.cpp (2pt) Write a program that asks the user to enter his/her name. Then begin a do while loop that asks the user to enter a number between 1 and 10. Have the random number generator produce a number 1 and 10. Display the user’s name and both numbers to the screen. Compare the two numbers and report if the entered number is greater than, less than, or the same as the generated number. Ask the user if he/she’d like...

  • Design a program that asks the user to enter a series of 20 numbers. The program...

    Design a program that asks the user to enter a series of 20 numbers. The program should store the numbers in the array and then display the following data: The lowest number in the array The highest number in the array The total of the number in the array The average of the numbers in the array PLEASE GIVE THE PSEUDOCODE AND CODE IN C PROGRAMMING

  • Project 1, Program Design 1. Write a C program replace.c that asks the user to enter...

    Project 1, Program Design 1. Write a C program replace.c that asks the user to enter a three-digit integer and then replace each digit by the sum of that digit plus 6 modulus 10. If the integer entered is less than 100 or greater than 999, output an error message and abort the program. A sample input/output: Enter a three-digit number: 928 Output: 584 2. Write a C program convert.c that displays menus for converting length and calculates the result....

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

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