Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
B.Tech S3 (R) (FT/WP) Examination November 2025 (2024 Scheme)QP 06PCCST303112503Part B · Q10a6 marksCO1Module 1asked in 2 papers

Question 10a

Convert the infix expression (A+B*C/D)-(E-F/G) to postfix form using a stack. Evaluate the resultant postfix expression by replacing A,B,C,D,E,F and G with 3,2,4,2,5,8 and 4.

Topics: Infix to postfix conversion, Evaluating postfix expressions

Watch this exact question run

Loading visualizer…

Solution

Assumptions:
  • Usual precedence: * and / above + and −; all four are left-associative.

Part 1 — convert (A+B*C/D)−(E−F/G) with an operator stack:

Conversion (generated by the conversion engine)checked by code
SymbolStackPostfix
((
A(A
+( +A
B( +A B
*( + *A B
C( + *A B C
/( + /A B C *
D( + /A B C * D
)emptyA B C * D / +
--A B C * D / +
(- (A B C * D / +
E- (A B C * D / + E
-- ( -A B C * D / + E
F- ( -A B C * D / + E F
/- ( - /A B C * D / + E F
G- ( - /A B C * D / + E F G
)-A B C * D / + E F G / -
endemptyA B C * D / + E F G / - -

Postfix: A B C * D / + E F G / - -

Part 2 — substitute A = 3, B = 2, C = 4, D = 2, E = 5, F = 8, G = 4 and evaluate the postfix form with an operand stack:

Evaluation (generated by the evaluation engine)checked by code
SymbolStackPostfix
((
A(A
+( +A
B( +A B
*( + *A B
C( + *A B C
/( + /A B C *
D( + /A B C * D
)emptyA B C * D / +
--A B C * D / +
(- (A B C * D / +
E- (A B C * D / + E
-- ( -A B C * D / + E
F- ( -A B C * D / + E F
/- ( - /A B C * D / + E F
G- ( - /A B C * D / + E F G
)-A B C * D / + E F G / -
endemptyA B C * D / + E F G / - -
SymbolActionStack
Apush 33
Bpush 23 2
Cpush 43 2 4
*2 * 4 = 83 8
Dpush 23 8 2
/8 / 2 = 43 4
+3 + 4 = 77
Epush 57 5
Fpush 87 5 8
Gpush 47 5 8 4
/8 / 4 = 27 5 2
-5 - 2 = 37 3
-7 - 3 = 44

Postfix: A B C * D / + E F G / - - · Result: 4 (integer division)

Pro tip

Check by direct substitution: (3 + 2·4/2) − (5 − 8/4) = (3 + 4) − (5 − 2) = 7 − 3 = 4. All divisions here are exact, so integer and real division give the same answer.

How to write it in the exam

  • Postfix: A B C * D / + E F G / − −
  • Evaluation: 2·4 = 8, 8/2 = 4, 3 + 4 = 7; 8/4 = 2, 5 − 2 = 3; 7 − 3 = 4.
  • Result = 4
How this answer was verified
  • tests/linear-expression.test.ts asserts the postfix string and the value 4
  • infix_to_postfix.c and postfix_eval.c produce the same output (part of 408 + 400 differential cases)
  • Hand check by direct substitution

Similar questions