Example input
Make a program that is a simple calculator that allows you to operate with two numbers using functions.
The program will read two integers entered from the keyboard and display the result of adding, subtracting, multiplying, and dividing those values.
The prototypes of the functions will be the following:
void ReadNumber (int *num);
void WriteResult(int num1, int num2, char operator, float result);
int add(int num1, int num2);
int Subtract(int num1, int num2);
int multiply(int num1, int num2);
float Divide(int num1, int num2);
Example output
Sure, here's the C code for the calculator program that meets the requirements specified in the task:
#include <stdio.h>
void ReadNumber(int *num);
void WriteResult(int num1, int num2, char operator, float result);
int add(int num1, int num2);
int Subtract(int num1, int num2);
int multiply(int num1, int num2);
float Divide(int num1, int num2);
int main() {
int num1, num2;
char operator;
float result;
ReadNumber(&num1);
ReadNumber(&num2);
operator = '+';
result = add(num1, num2);
WriteResult(num1, num2, operator, result);
operator = '-';
result = Subtract(num1, num2);
WriteResult(num1, num2, operator, result);
operator = '*';
result = multiply(num1, num2);
WriteResult(num1, num2, operator, result);
operator = '/';
result = Divide(num1, num2);
WriteResult(num1, num2, operator, result);
return 0;
}
void ReadNumber(int *num) {
printf("Enter a number: ");
scanf("%d", num);
}
void WriteResult(int num1, int num2, char operator, float result) {
printf("%d %c %d = %.2f\n", num1, operator, num2, result);
}
int add(int num1, int num2) {
return num1 + num2;
}
int Subtract(int num1, int num2) {
return num1 - num2;
}
int multiply(int num1, int num2) {
return num1 * num2;
}
float Divide(int num1, int num2) {
return (float)num1 / (float)num2;
}
The program starts by declaring the necessary variables, including two integers for the input values, a character for the operator, and a float for the result. It then calls the ReadNumber function twice to get the input values from the user.
Next, the program performs each of the four arithmetic operations by calling the appropriate function and storing the result in the result variable. The operator character is also set to the appropriate value for each operation.
Finally, the program calls the WriteResult function to display the result of each operation on the screen. This function takes four arguments: the two input values, the operator, and the result of the operation. The function uses printf to display the values in the desired format.
The ReadNumber function simply prompts the user to enter a number and reads the value into the specified integer variable using scanf.
The WriteResult function formats and displays the input values, operator, and result in the specified format using printf.
The add, Subtract, multiply, and Divide functions perform the corresponding arithmetic operation on the two input values and return the result.
Overall, this program meets the requirements specified in the task and should work correctly for most input values. However, it does not handle division by zero or other potential edge cases.