WAP in C to create a simple Calculator?

// This program performs addition, subtraction, multiplication or division depending the input from user.

#include<stdio.h>
#include<conio.h>
int main()
{

char operator;
double a,b;
clrscr();
printf(“Enter an operator (+, -, *, /): “);
scanf(“%c”, &operator);
printf(“Enter two operands: “);
scanf(“%lf %lf”,&a, &b);
switch(operator)
{
case ‘+’:
printf(“%.1lf + %.1lf = %.1lf”,a, b, a+b);
break;

case ‘-‘:
printf(“%.1lf – %.1lf = %.1lf”,a, b, a-b);
break;

case ‘*’:
printf(“%.1lf * %.1lf = %.1lf”,a, b, a*b);
break;

case ‘/’:
printf(“%.1lf / %.1lf = %.1lf”,a, b, a/b);
break;

// operator is doesn’t match any case constant (+, -, *, /)
default:
printf(“Error! operator is not correct”);
}
getch();
return(0);
}

Output :-

switchswitch1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s