C Exercises: Reads an expression and evaluates
Evaluate an arithmetic expression
Write a C program that reads an expression and evaluates it.
Terms and conditions:
The expression consists of numerical values, operators and parentheses, and the ends with '='.
The operators includes +, -, *, / where, represents, addition, subtraction, multiplication and division.
When two operators have the same precedence, they are applied to left to right.
You may assume that there is no division by zero.
All calculation is performed as integers, and after the decimal point should be truncated
Length of the expression will not exceed 100.
-1 × 10 9 <= intermediate results of computation <= 10 9
Sample Input:
4
10-2*3=
8*(8+2-5)=
Sample Solution:
C Code:
Sample Output:
Input an expression using +, -, *, / operators: 1+6*8-4/2 47
Sample Output:
Input an expression using +, -, *, / operators: 25/5-6*7+2 -35
Sample Output:
Input an expression using +, -, *, / operators: 9+6+(5*2)-5 20
Flowchart:


For more Practice: Solve these Related Problems:
- Write a C program to parse and evaluate an arithmetic expression containing +, -, *, and / without using eval.
- Write a C program to implement a simple calculator that reads a string expression and computes the result using stacks.
- Write a C program to evaluate an arithmetic expression by converting it to postfix notation and then computing the result.
- Write a C program to use recursion and operator precedence rules to evaluate a complex arithmetic expression provided by the user.
C programming Code Editor:
Previous: Write a C program, which reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.
Next: C Variable Type Exercises Home
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.