The Program in C to Built Mini Calculator using function is given below:
#include <stdio.h>
// Function for addition
int add(int a, int b) {
return a + b;
}
// Function for subtraction
int subtract(int a, int b) {
return a - b;
}
// Function for multiplication
int multiply(int a, int b) {
return a * b;
}
// Function for division
float divide(int a, int b) {
return (float)a / b;
}
int main() {
int choice, a, b;
float result;
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("Enter your choice: ");
scanf("%d", &choice);
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
switch(choice) {
case 1:
result = add(a, b);
printf("Result: %d\n", result);
break;
case 2:
result = subtract(a, b);
printf("Result: %d\n", result);
break;
case 3:
result = multiply(a, b);
printf("Result: %d\n", result);
break;
case 4:
result = divide(a, b);
printf("Result: %.2f\n", result);
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Output:
- Addition
- Subtraction
- Multiplication
- Division
Enter your choice: 3
Enter two numbers: 5
6
Result: 6
Pro-Tips💡
This program allows the user to select an operation (addition, subtraction, multiplication, or division) and then enter two numbers.
The corresponding function is called and the result is displayed.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.