Please look at image below. Chapter 6 activity (Protected View) - Word Mazen Moh
ID: 3723668 • Letter: P
Question
Please look at image below.
Chapter 6 activity (Protected View) - Word Mazen Mohamed - File Home InsertDraw DesignLayout References Mailings Review VieACROBAT Tell me what you want to da Share PROTECTED VIEW Be careful-files from the Internet can contain viruses. Unless you need to edit, it's safer to stay in Protected View Enble Editing Navigation In class exercise: math method Description Search dacument Write a program that can calculate a simple math equation using a sub method. The sub method you write will have 2 double parameters and 1 integer parameter. The return type will be double. The method will use the two doubles as the equation numbers and will use the integer to determine which operation to use (1 being addition, 2 subtraction, 3 multiplication, 4 division). The program will then return the result of the equation as a double. In the main method, ask the user to supply values for the 3 parameters, then pass them to the method before displaying the result. At the end of each run of the program, ask the user whether or not they would like to continue. If they respond "true", continue the program, otherwise end it. Headings Pages Results Example run Input number 1 30 Input number 2 50 Equation? The result is: 150.0 Continue? False Page 1 of 1 230 woirds + 100% 4:42 AMExplanation / Answer
#include<stdio.h>
float sub(float a, float b, int t);
int main()
{
int op=0, set=1;
float n1,n2,result;
while (set)
{
printf("Enter the 2 float values : ");
scanf("%f",&n1);
scanf("%f",&n2);
printf(" Enter the number from following: ");
printf("1. Addition 2.Subtraction 3.Multiplication 4.Division ");
scanf("%d",&op);
result = sub( n1, n2, op);
printf("The result is : %f ",result);
printf("enter 1 to continue or 0 to exit ");
scanf("%d",&set);
}
return 0;
}
float sub(float a, float b, int t)
{
if (t==1)
return a+b;
else if (t==2)
return a-b;
else if (t==3)
return a*b;
else if(t==4)
return a/b;
else
printf("enter valid number ");
}
}
OR we can use Switch case also as follows
float sub(float a, float b, int t)
{
switch(t)
{
case 1: return a+b;
break; // actually no need of break since we are returning but as per syntax mentioned
case 2: return a-b;
break;
case 3: return a*b;
break;
case 4: return a/b;
break;
default : printf("invalid"); break;
}
}