Hello everyone . So i tried to make a program that allow the user to select if he wants to add subtract multiply or devide . This is the program :

#include <stdio.h>

int main()

{

int num1, num2, num3, num4, first, second, add, subtract, multiply;

float divide;

printf("Would you like to Add , subtract , multiply or divide?\n ");

printf("Please enter 1 to add , 2 to subtract , 3 to multiply or 4 to divide\n");

scanf("%d,%d,%d,%d", &num1, &num2, &num3, &num4);

if (num1 == 1)

{

// Add = first + second;

}

else if (num1 == 2)

{

// Subtract = first - second;

}

else if (num1 == 3)

{

// Multiply = first * second;

}

else (num1 == 4);

{

// Divide = first / (float)second;

}

printf("Enter two numbers");

scanf("%d%d", &first, &second);

add = first + second;

subtract = first - second;

multiply = first * second;

divide = first / (float)second; //typecasting

printf("Sum = %d\n", add);

printf("Difference = %d\n", subtract);

printf("Multiplication = %d\n", multiply);

printf("Division = %.2f\n", divide);

return 0;

}

Its working well but instead of working on the selection of the user my program give this answer :

Would you like to Add , subtract , multiply or divide?

Please enter 1 to add , 2 to subtract , 3 to multiply or 4 to divide

1 <=== (i pressed 1)

Enter two numbers5 < (then it says to the user to give 2 numbers)

5 < ---( i typed 5 and 5 )

Sum = 10 ( but in the end instead of gving the result of the add)

Difference = 0 ( function , it gives the result for all of them )

Multiplication = 25

Division = 1.00

--------------------------------

Process exited after 2.262 seconds with return value 0

Press any key to continue . . .

Any advice on what should i do so that the program will give only the selected choice to the user is apreciated

#include <stdio.h>

int main()

{

int num1, num2, num3, num4, first, second, add, subtract, multiply;

float divide;

printf("Would you like to Add , subtract , multiply or divide?\n ");

printf("Please enter 1 to add , 2 to subtract , 3 to multiply or 4 to divide\n");

scanf("%d,%d,%d,%d", &num1, &num2, &num3, &num4);

if (num1 == 1)

{

// Add = first + second;

}

else if (num1 == 2)

{

// Subtract = first - second;

}

else if (num1 == 3)

{

// Multiply = first * second;

}

else (num1 == 4);

{

// Divide = first / (float)second;

}

printf("Enter two numbers");

scanf("%d%d", &first, &second);

add = first + second;

subtract = first - second;

multiply = first * second;

divide = first / (float)second; //typecasting

printf("Sum = %d\n", add);

printf("Difference = %d\n", subtract);

printf("Multiplication = %d\n", multiply);

printf("Division = %.2f\n", divide);

return 0;

}

Its working well but instead of working on the selection of the user my program give this answer :

Would you like to Add , subtract , multiply or divide?

Please enter 1 to add , 2 to subtract , 3 to multiply or 4 to divide

1 <=== (i pressed 1)

Enter two numbers5 < (then it says to the user to give 2 numbers)

5 < ---( i typed 5 and 5 )

Sum = 10 ( but in the end instead of gving the result of the add)

Difference = 0 ( function , it gives the result for all of them )

Multiplication = 25

Division = 1.00

--------------------------------

Process exited after 2.262 seconds with return value 0

Press any key to continue . . .

Any advice on what should i do so that the program will give only the selected choice to the user is apreciated

JOHN2172000 wrote: |
---|

Its working well |

You have an

`if` ... `else` `if`

structure set up, but you don't actually use it; (everything inside is in comments).When you have gone through that

`if` ... `else` `if`

structure you do all the operations and then print all the results. Does that seem right?Maybe you should reorder:

- ask for two numbers;

- ask for the operation;

- put your

`if` ... `else` `if`

arrangement here and calculate and output the required operation in the right place.PLEASE USE CODE TAGS.

Registered users can post here. Sign in or register to post.