- You should really consider making functions for each mathematical operation and keep the
main()
simple.
As an example:
voidfloat addition(){
float num1, num2;
float result;
printf("Enter first number: "num2);{
return num1 scanf("%f",+ &num1);num2;
}
printf("Enter second number: ");
scanf("%f", &num2);//or
void addition(float num1, float printf("\n"num2);{
float result = num1 + num2;
printf("%.2f + %.2f = %"%0.2f\n", num1, num22f", result);
}
This wayIn the second example, you also create and destroy num1
num2
and result
each function call.
Consider asking the user if they would like to return to the menu, in case they want to do multiple calculations.
You don't need multiple
printf
statements for the menu options. Simply continuing as such is needed:printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n\n");