#include <stdio.h>
void calc(char opt, int n1, int n2, float *res) {
switch (opt) {
case '+':
*res = n1 + n2; // add two numbers
printf(" Addition of %d and %d is: %.2f\n", n1, n2, *res);
break;
case '-':
*res = n1 - n2; // subtract two numbers
printf(" Subtraction of %d and %d is: %.2f\n", n1, n2, *res);
break;
case '*':
*res = n1 * n2; // multiply two numbers
printf(" Multiplication of %d and %d is: %.2f\n", n1, n2, *res);
break;
case '/':
if (n2 == 0) {
printf("\n Divisor cannot be zero. Please enter another value: ");
scanf("%d", &n2);
}
*res = n1 / (float)n2; // divide two numbers
printf(" Division of %d and %d is: %.2f\n", n1, n2, *res);
break;
default:
printf(" Something is wrong!! Please check the options\n");
}
}
char options() {
char opt;
printf(" Choose an operator (+, -, *, /) to perform the operation in C Calculator: ");
scanf(" %c", &opt); // note the space before %c to skip whitespace characters
getchar(); // consume newline character from the input buffer
if (opt == '/')
printf(" You have selected: Division\n");
else if (opt == '*')
printf(" You have selected: Multiplication\n");
else if (opt == '-')
printf(" You have selected: Subtraction\n");
else if (opt == '+')
printf(" You have selected: Addition\n");
return opt;
}
void projectInfo() {
printf("\n\t\t\t\t*************** Calculator Program ***************\n");
printf("\n\t\t\t\t------------- Submitted By ---------------\n");
printf("\n\t\t\t\t------------- Sharifa Siddika ---------------\n");
printf("\n\t\t\t\t----------- Submitted To ---------------\n");
printf("\n\t\t\t\t--------- Abdullah Ibn Obaidullah --------\n\n\n");
}
int main() {
projectInfo();
// declare local variables
int n1, n2;
float res;
int choice = 1;
while (choice == 1) {
int cases;
char opt;
printf("\nPlease enter choice to proceed:\n");
printf("1. Project Info\n");
printf("2. Open Calculator\n");
printf("3. Exit\n");
scanf("%d", &cases);
switch (cases) {
case 1:
projectInfo();
continue;
case 2:
opt = options();
printf(" Enter the first number: ");
scanf("%d", &n1);
printf(" Enter the second number: ");
scanf("%d", &n2);
calc(opt, n1, n2, &res);
continue;
case 3:
choice = 2;
break;
}
}
printf("\n\n\n\t\t\t ************ THANKS ***************\n");
return 0;
}