Skip to content

Instantly share code, notes, and snippets.

@YousufProjs-exe
Created February 26, 2026 14:05
Show Gist options
  • Select an option

  • Save YousufProjs-exe/c73dc4f1f7c952ce5a6dc59321428ef3 to your computer and use it in GitHub Desktop.

Select an option

Save YousufProjs-exe/c73dc4f1f7c952ce5a6dc59321428ef3 to your computer and use it in GitHub Desktop.
The calculator prompts users to enter two numbers and select an operation. It then performs the calculation and displays the result. The program is designed with clarity in mind, making it an excellent resource for beginners learning C programming concepts such as input/output handling, conditional statements, loops, and basic arithmetic logic.
#include <stdio.h>
int main()
{
int a, b, opt, res;
// declaring variables
printf("Welcome To Switch Case Calculator\n");
// Welcome command / Message
printf("Enter first number: ");
scanf("%d", &a);
// asking user for first number (first Input)
printf("Enter second number: ");
scanf("%d", &b);
// asking user for second number (Second Input)
printf("Arithmetic Operations: \n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulus Division\n");
// Setting options
printf("Enter your choice: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
res = a + b;
printf("Addition: %d\n", res);
break;
case 2:
res = a - b;
printf("Subtraction: %d\n", res);
break;
case 3:
res = a * b;
printf("Multiplication: %d\n", res);
break;
case 4:
if(b != 0) {
res = a / b;
printf("Division: %d\n", res);
} else {
printf("Error: Division by zero!\n");
}
break;
case 5:
if(b != 0) {
res = a % b;
printf("Modulus Division: %d\n", res);
} else {
printf("Error: Division by zero!\n");
}
break;
default:
printf("Invalid Option Selected\n");
// default Case for Invalid attempts
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment