C – program to create Simple Calculator using Switch Case

#include <stdio.h>
int main()
{
int num1, num2;
float result;
char ch;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Choose operator to perform operations: ");
scanf(" %c", &ch);
printf("Enter second number: ");
scanf("%d", &num2);
result = 0;

switch (ch)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0)
{
printf("Divison with zero is not possible.");
}
else
{
result = num1 / num2;
}
break;
case '%':
if (num2 == 0)
{
printf("Modulos of a number with zero is not posible.");
}
else
{
result = num1 % num2;

}
break;
default:
printf("Invalid operation\n");
}
printf("%d %c %d = %f\n", num1, ch, num2, result);
return 0;
}
Scroll to Top
logo
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.