Calculator
- switch
#include<stdio.h>
int main()
{
int a,b;
char o;
printf("Enter the numbers ");
scanf("%d %d",&a,&b);
printf("Enter the operation ");
scanf(" %c",&o);//space has to be given to enter character
switch(o)
{
case '+' :
printf("The sum is %d",a+b);
break;
case '-' :
printf("The difference is %d",a-b);
break;
case '*' :
printf("The product is %d",a*b);
break;
case '/' :
printf("The division is %d",a/b);
break;
case '%' :
printf("The remainder is %d",a%b);
break;
}
return 0;
}
Output -
Enter the numbers 4
5
Enter the operation +
The sum is 9
- If-else
#include <stdio.h>
int main()
{ int a,b;
char o;
printf("Enter the numbers ");
scanf("%d %d",&a,&b);
printf("Enter the operation ");
scanf(" %c",&o);//space has to be given to enter character
if(o='+')
{ printf("The sum is %d",a+b); }
else if (o='-')
{ printf("The difference is %d",a-b); }
else if (o='*')
{ printf("The product is %d",a*b); }
else if (o='/')
{ printf("The division is %d",a/b); }
else if (o='%')
{ printf("The remainder is %d",a%b); }
else
printf("Enter valid operator");
return 0;
}
Output -
Enter the numbers 4
5
Enter the operation +
The sum is 9
4321
0 Comments