Skip to content

Commit 20e31f8

Browse files
committed
error checking
1 parent 1cdab47 commit 20e31f8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

prog.6.8A.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Program to evaluate simple expressions of the form
2+
number operator number */
3+
4+
#include <stdio.h>
5+
6+
int main (void)
7+
{
8+
float value1, value2;
9+
char operator;
10+
11+
printf ("Type in your expression.\n");
12+
scanf ("%f %c %f", &value1, &operator, &value2);
13+
14+
if ( operator == '+' )
15+
printf ("%.2f\n", value1 + value2);
16+
else if ( operator == '-' )
17+
printf ("%.2f\n", value1 - value2);
18+
else if ( operator == '*' )
19+
printf ("%.2f\n", value1 * value2);
20+
else if ( operator == '/' )
21+
if ( value2 == 0 )
22+
printf ("Division by zero.\n");
23+
else
24+
printf ("%.2f\n", value1 / value2);
25+
else
26+
printf ("Unknown operator.\n");
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)