Updated (12 Feb 2023)

Postfix & Prefix Evaluator

This is a simple Prefix or Postfix Evaluator.


Enter the Postfix or Prefix expression below in box and press Evaluate
Note: Enter the number and operators seperated with space " "

Evaluated :

By Raj

Sr. no. Expression Stack

Algorithm used

Postfix Evaluation

  • Step 1: Add a ")" at the end of the postfix expression
  • Step 2: Scan every character of the postfix expression and repeat Step 3 and 4 until ")" is encountered.
  • Step 3: IF an operand is encountered, Push it on the stack
    IF an operator O is encountered, then
    • a: Pop the top two elements from the stack as A and B
    • b: Evaluate B O A, where A is the topmost element and B is the element below A.
    • c: Push the result of evaluation on the stack
      [END OF IF]
  • Step 4: Set result = stack's top elements
  • Step 5: Exit

Prefix Evaluation

  • Step 1: Get Prefix expression as it is
  • Step 2: Repeat untill all the characters in prefix
    expression have been scanned
    • a: Read the prefix expression from right to left one at a time
    • b: If the readed character is an operand, push it on the stack
    • c: If the readed character is an operator, then
      • i: pop two values from the stack.
      • ii: Apply the operation on the operands.
      • iii: Push the result onto the stack.
  • Step 3: Exit