Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Patterns

Array

2D Array

String Function Example

Pointers

Recursion Function

Structure

Excersises

Others


C Program to Create Simple Calculator using Switch


In this program, we are getting operator and two numbers as input from the user. Then,it performs the calculation of the two values depending upon the given operator.

Example : pgm.c

#include<stdio.h>
void main()
{
  char ch;
  int a,b;
  printf("Enter an Operator ( +  -  *  / ) : ");
  scanf("%c",&ch);
  printf("Enter First Value : ");
  scanf("%d",&a);
  printf("Enter Second Value : ");
  scanf("%d",&b);

  switch(ch)
  {
     case '+':
      printf("%d + %d = %d",a,b,(a+b));
      break;

     case '-':
      printf("%d - %d = %d",a,b,(a-b));
      break;

     case '*':
      printf("%d * %d = %d",a,b,(a*b));
      break;

     case '/':
      printf("%d / %d = %d",a,b,(a/b));
      break;

     default:
      printf("%c is not correct",ch);
      break;

  }

Output:

Enter an operator (+,-,*,/) : +
Enter First Value : 69
Enter Second Value : 36
69 + 36 = 105