Calculator in C language

Kamalveer Singh
3 min readOct 6, 2024

--

As you know making a calculator is one of the first program which everyone makes but is it really that good??

Let’s first make a program which is basic calculator.

#include <ctype.h>
#include <stdio.h>

int main(void) {
int a, b, choice;

printf("Enter number A: ");
scanf("%d", &a);

printf("Enter number B: ");
scanf("%d", &b);

printf("\nSelect the operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Remainder\n\n");
printf("Enter here: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("%d + %d = %d\n", a, b, a + b);
break;

case 2:
printf("%d - %d = %d\n", a, b, a - b);
break;

case 3:
printf("%d x %d = %d\n", a, b, a * b);
break;

case 4:
printf("%d / %d = %d\n", a, b, a / b);
break;

case 5:
printf("Remainder when %d is divided by %d = %d\n", a, b, a % b);
break;

default:
printf("Error: Invalid choice. Please select a number between 1 and 5.\n");
break;
}

return 0;
}
Program output for multiplication

But what if the second number is zero, then how can you find the division and remainder.

Dividing by Zero exception

Divide by Zero

To handle this, modify the program like this →

case 4: 
if (b != 0) {
printf("%d / %d = %d\n", a, b, a / b);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;

case 5:
if (b != 0) {
printf("Remainder when %d is divided by %d = %d\n", a, b, a % b);
} else {
printf("Error: Division by zero is not allowed for remainder calculation.\n");
}
break;

Better Code

Now our program is good but doesn’t handle if we some character and it also doesn’t ask the user to continue to use it.

#include <stdio.h>

#define ADDITION 1
#define SUBTRACTION 2
#define MULTIPLICATION 3
#define DIVISION 4
#define REMAINDER 5

int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int safe_divide(int a, int b) {
if (b == 0) {
printf("Error: Division by zero is not allowed.\n");
return 0; // or some error code
}
return a / b;
}
int remainder(int a, int b) {
if (b == 0) {
printf("Error: Division by zero is not allowed for remainder calculation.\n");
return 0; // or some error code
}
return a % b;
}

int main(void) {
int a, b, choice;
char continueCalculation;

do {
printf("Enter number A: ");
scanf("%d", &a);
printf("Enter number B: ");
scanf("%d", &b);

printf("\nSelect the operation:\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Remainder\n");
printf("Enter here: ");
scanf("%d", &choice);

switch (choice) {
case ADDITION:
printf("Result: %d + %d = %d\n", a, b, add(a, b));
break;
case SUBTRACTION:
printf("Result: %d - %d = %d\n", a, b, subtract(a, b));
break;
case MULTIPLICATION:
printf("Result: %d x %d = %d\n", a, b, multiply(a, b));
break;
case DIVISION:
printf("Result: %d / %d = %d\n", a, b, safe_divide(a, b));
break;
case REMAINDER:
printf("Remainder when %d is divided by %d = %d\n", a, b, remainder(a, b));
break;
default:
printf("Error: Invalid choice. Please select a number between 1 and 5.\n");
break;
}

printf("Do you want to perform another calculation? (y/n): ");
scanf(" %c", &continueCalculation); // Note the space before %c to consume any newline character
} while (continueCalculation == 'y' || continueCalculation == 'Y');

return 0;
}

Now, this can also be improved further, and I would like to see your takes on the same.

I made a better version of this blog, but it got removed because I accidently closed the tab and no drafts in it. I hope this one also helps : )

--

--

Kamalveer Singh
Kamalveer Singh

Written by Kamalveer Singh

0 Followers

I love Computer Science <3

No responses yet