First, we declare two integer variables
num2 to store the two numbers to be operated upon.
Then, we declare a
result to store the result of the arithmetic operation.
Next, we declare a character variable
operator to store the arithmetic operator to be used.
We then use the
printf function to prompt the user to enter two numbers and an operator.
scanf function is used to read the input values for the two numbers and the operator.
Next, we use a
switch statement to perform the arithmetic operation based on the value of the
In each case of the
switch statement, we perform the respective arithmetic operation and print the result using the
If the value of the
operator variable is not one of the four arithmetic operators (+, -, *, /), the default case is executed and an error message is displayed.
return statement is used to return a value of 0 to indicate that the program has run successfully.
Arithmetic operations such as addition, subtraction, multiplication, and division C program
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
result = num1 + num2;
result = num1 - num2;
result = num1 * num2;
result = (float) num1 / num2;