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(+)...

  • In Small Basic Write a program that asks the user how many numbers s/he wishes to...

    In Small Basic Write a program that asks the user how many numbers s/he wishes to enter into an array and then enters a loop and proceeds to get those numbers from the user and enter them into the array. Once the array has been filled with the numbers, your program then asks the user to make a choice indicating what s/he wishes to do with the numbers in the array. The choices are: TextWindow.WriteLine("Enter 1 to compute and display...

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

  • Write a program that asks the user how many integers they would like to enter. You...

    Write a program that asks the user how many integers they would like to enter. You can assume they will enter an integer >= 1. The program will then prompt the user to enter that many integers. After all the numbers have been entered, the program should display the largest and smallest of those numbers (no, you cannot use lists, or any other material we haven't covered). When you run your program it should match the following format: How many...

  • Write a program that asks the user to enter two numbers. Then compare the two numbers....

    Write a program that asks the user to enter two numbers. Then compare the two numbers. Display either:     "The first number is larger", or     "The second number is larger", or     "The numbers are the same".

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