Skip to content

Instantly share code, notes, and snippets.

@krishnaprajapat
Created July 22, 2018 13:19
Show Gist options
  • Save krishnaprajapat/3e023581a50231221803b0525a69faac to your computer and use it in GitHub Desktop.
Save krishnaprajapat/3e023581a50231221803b0525a69faac to your computer and use it in GitHub Desktop.
Simple calculator using switch in c
#include<stdio.h>
void add(int a,int b)
{
printf("\n %d+%d=%d",a,b,a+b);
}
void sub(int a,int b)
{
printf("\n %d-%d=%d",a,b,a-b);
}
void mult(int a,int b)
{
printf("\n %d*%d=%d",a,b,a*b);
}
void div(int a,int b)
{
printf("\n %d/%d=%d",a,b,a/b);
}
int main()
{
int n,a,b;
printf("\n enter first number");
scanf("%d",&a);
printf("\n enter second number");
scanf("%d",&b);
printf("\n enter a choice");
printf("\n1:+\n2:-\n3:*\n4:/");
scanf("%d",&n);
switch(n)
{
case 1:
add(a,b);
break;
case 2:
sub(a,b);
break;
case 3:
mult(a,b);
break;
case 4:
div(a,b);
default:
printf("\n invalid choice");
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment