In: Computer Science
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should prompt the user for the operation they wish to perform followed by the numbers they wish to operate on. You should have a function for each operation and use branches to determine which function to call.
I need this to make any integers given, into decimal numbers, such as 3 to 3.0, or 2 to 2.0, also, so that I can multiply or add things like 2.5 + 3.0 or 2.5 * 2.5, etc.
Thank you, please see my code below.
-------------------------------------------------------------
MY CODE:*****
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//Functions
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
float div(int a, int b)
{
return a * 1.0 / b;
}
int main()
{
int result, a, b, choice;
float res;
//User prompts
printf("Math Calculator: What type of operation would you like me to use? (You will choose numbers after selection) : \n1.Addition\n2.Subtraction\n3.Multiply\n4.Divide\n");
scanf("%d", &choice);
printf("Enter first number : ");
scanf("%d", &a);
printf("Enter second number : ");
scanf("%d", &b);
//Cases
switch (choice) {
case 1:
result = add(a, b);
printf("The answer equals %d", result);
break;
case 2:
result = sub(a, b);
printf("The answer equals %d", result);
break;
case 3:
result = mul(a, b);
printf("The answer equals %d", result);
break;
case 4:
res = div(a, b);
printf("The answer equals %f", res);
break;
default:
printf("You did not select a valid operation from above.");
break;
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//Functions
int add(float a, float b)
{
return a + b;
}
int sub(float a, float b)
{
return a - b;
}
int mul(float a, float b)
{
return a * b;
}
float div(float a, float b)
{
return a * 1.0 / b;
}
int main()
{
int result, choice;
float res,a,b;
//User prompts
printf("Math Calculator: What type of operation would you like me to use? (You will choose numbers after selection) : \n1.Addition\n2.Subtraction\n3.Multiply\n4.Divide\n");
scanf("%d", &choice);
printf("Enter first number : ");
scanf("%f", &a);
printf("Enter second number : ");
scanf("%f", &b);
//Cases
switch (choice) {
case 1:
res = add(a, b);
printf("The answer equals %f",res);
break;
case 2:
res = sub(a, b);
printf("The answer equals %f", res);
break;
case 3:
res = mul(a, b);
printf("The answer equals %f", res);
break;
case 4:
res = div(a, b);
printf("The answer equals %f", res);
break;
default:
printf("You did not select a valid operation from above.");
break;
}
return 0;
}
// to accept 2 as 2.0 we need to change the data types to float
Note : If you like my answer please rate and help me it is very Imp for me