Question: Can someone read through my code and tell me why it is not working? Here is the assignment: Write a C program that asks the
Can someone read through my code and tell me why it is not working?
Here is the assignment:
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. Your code should include a switch structure in addition to whatever else you need to make your code run as shown in the sample runs below.
Here is my code:
#include
#include
#include
int main()
{
float Num1, Num2, Results;
int DisplayInput; //variable declaration
printf ("Please enter your first number. "); //recieves input for first number
scanf ("%d", &Num1);
printf ("Please enter your second number. "); //receives input for second number
scanf ("%d", &Num2);
printf ("Please enter the number corresponding to the operation you wish to perform. "); //displays chart
printf ("1\tto print the sum of the sumbers. ");
printf ("2\tto print the product (multiplication) of the numbers. ");
printf ("3\tto print the division of the first number divided by the second number. ");
printf ("4\tto print the square root of the first number. ");
printf ("5\texit program. ");
scanf ("%d", &DisplayInput); //recievies input for chart selection
if (DisplayInput == 5)
{
printf ("Have a great day! ");
return 0;
}
else
{
switch(DisplayInput)
{
case 1:
Results=Num1+Num2;
printf ("After your operation, your result is %f", Results);
break;
case 2:
Results=Num1*Num2;
printf ("After your operation, your result is %f", Results);
break;
case 3:
if (Num2==0)
printf ("You cannot divide by zero. ");
else
Results=Num1/Num2;
printf ("After your operation, your result is %f", Results);
break;
case 4:
if (Num1<0)
printf ("You cannot take the square root of a number less than zero. ");
else
Results=sqrt(Num1);
printf ("After your operation, your result is %f", Results);
break;
default:
Results=0;
break;
}
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
