Prefix Evaluator

Evaluate a prefix expression using stack, and see the step-by-step process used to achieve the result.

Learn More

Selected Data Record:

A Data Record is a set of calculator entries that are stored in your web browser's Local Storage. If a Data Record is currently selected in the "Data" tab, this line will list the name you gave to that data record. If no data record is selected, or you have no entries stored for this calculator, the line will display "None".

DataData recordData recordSelected data record: None
Example notations:

Example prefix expressions:

To see an example of how the Prefix Evaluator works, and what types of expressions the calculator is set up to handle, select a pretfix expression from the drop-down menu. To clear the expression field to enter your own prefix expression, select "Example Problems" or click the "Reset" button.

Prefix expression:

Prefix expression:

Enter a prefix expression that fits within the following guidelines:

  • Include a single space between numbers or operators.
  • Numbers and operators only (no letters or variables).
  • Contains only numbers, decimal points, and these valid characters: ^ * / + - .
  • Numbers with a leading decimal point must be preceded by a zero (enter .5 as 0.5).
#
Result:

Result:

This line will display the result of the prefix evaluation.

If you would like to save the current entries to the secure online database, tap or click on the Data tab, select "New Data Record", give the data record a name, then tap or click the Save button. To save changes to previously saved entries, simply tap the Save button. Please select and "Clear" any data records you no longer need.

Related Calculators

infix to prefix converterinfix to postfix converterpostfix evaluator

Help and Tools

Learn

How to evaluate prefix expression using stack.

How to Evaluate a Prefix Expression Using Stack

In case you're not familiar, a stack is a collection or list wherein the last element added to the stack is always the first element to be removed.

When evaluating prefix expressions, using a stack to temporarily store operands is necessary because as we are evaluating each character of the prefix expression from right to left, we can't instantly know what operation will be performed on the two operands. Therefore we need to temporarily add (push) operands to the stack and only remove (pop) them from the stack once we know what operation will be performed on them.

So now that you know what a stack is and why it is used, here is the process for evaluating a postfix expression using stack.

Moving from right to left, one character at a time, if a character is an operand (number), push it to the top of the stack.

Otherwise, if a character is an operator (^ * / + -), pop (remove) the top element from the stack to form the operator's left operand, and then pop the next top element from the stack to form the operator's right operand. Finally, solve the expression formed by the operator and its operands, and push the result to the top of the stack.

Repeat the above until all characters have been processed, at which point the last element remaining in the stack becomes the result.

Prefix Evaluation Examples

Here are a couple of examples of how to evaluate prefix expressions using the stack method.

Example #1: + - 20 * 3 4 1

+ - 20 * 3 4 1

The first character scanned is "1", which is an operand, so push it to the stack.

1
  
StackExpression

+ - 20 * 3 4 1

The next character scanned is "4", which is an operand, so push it to the stack.

4
1
  
StackExpression

+ - 20 * 3 4 1

The next character scanned is "3", which is an operand, so push it to the stack.

3
4
1
  
StackExpression

+ - 20 * 3 4 1

The next character scanned is "*", which is an operator, so pop its two operands from the stack. Pop 4 from the stack for the left operand and then pop 3 from the stack to make the right operand.

1
 3 * 4 = 12 
StackExpression

Next, push the result of 3 * 4 (12) to the stack.

12
1
  
StackExpression

+ - 20 * 3 4 1

The next character scanned is "20", which is an operand, so push it to the stack.

20
12
1
  
StackExpression

+ - 20 * 3 4 1

The next character scanned is "-", which is an operator, so pop its two operands from the stack. Pop 12 from the stack for the left operand and then pop 20 from the stack to make the right operand.

1
 20 - 12 = 8 
StackExpression

Next, push the result of 20 - 12 (8) to the stack.

8
1
  
StackExpression

+ - 20 * 3 4 1

The next character scanned is "+", which is an operator, so pop its two operands from the stack. Pop 1 from the stack for the left operand and then pop 8 from the stack to make the right operand.

 
 8 + 1 = 9 
StackExpression

Next, push the result of 8 + 1 (9) to the stack.

9
  
StackExpression

Since we are done scanning characters, the remaining element in the stack (9) becomes the result of the prefix evaluation.

prefix notation: + - 20 * 3 4 1
Result: 9

Example #2: - * 3 + 3 7 / ^ 4 2 2

- * 3 + 3 7 / ^ 4 2 2

The first character scanned is "2", which is an operand, so push it to the stack.

2
  
StackExpression

- * 3 + 3 7 / ^ 4 2 2

The next character scanned is "2", which is an operand, so push it to the stack.

2
2
  
StackExpression

- * 3 + 3 7 / ^ 4 2 2

The next character scanned is "4", which is an operand, so push it to the stack.

4
2
2
  
StackExpression

- * 3 + 3 7 / ^ 4 2 2

The next character scanned is "^", which is an operator, so pop its two operands from the stack. Pop 2 from the stack for the left operand and then pop 4 from the stack to make the right operand.

2
 4 ^ 2 = 16 
StackExpression

Next, push the result of 4 ^ 2 (16) to the stack.

16
2
  
StackExpression

- * 3 + 3 7 / ^ 4 2 2

The next character scanned is "/", which is an operator, so pop its two operands from the stack. Pop 2 from the stack for the left operand and then pop 16 from the stack to make the right operand.

 
 16 / 2 = 8 
StackExpression

Next, push the result of 16 / 2 (8) to the stack.

8
  
StackExpression

- * 3 + 3 7 / ^ 4 2 2

The next character scanned is "7", which is an operand, so push it to the stack.

7
8
  
StackExpression

- * 3 + 3 7 / ^ 4 2 2

The next character scanned is "3", which is an operand, so push it to the stack.

3
7
8
  
StackExpression

- * 3 + 3 7 / ^ 4 2 2

The next character scanned is "+", which is an operator, so pop its two operands from the stack. Pop 7 from the stack for the left operand and then pop 3 from the stack to make the right operand.

8
 3 + 7 = 10 
StackExpression

Next, push the result of 3 + 7 (10) to the stack.

10
8
  
StackExpression

- * 3 + 3 7 / ^ 4 2 2

The next character scanned is "3", which is an operand, so push it to the stack.

3
10
8
  
StackExpression

- * 3 + 3 7 / ^ 4 2 2

The next character scanned is "*", which is an operator, so pop its two operands from the stack. Pop 10 from the stack for the left operand and then pop 3 from the stack to make the right operand.

8
 3 * 10 = 30 
StackExpression

Next, push the result of 3 * 10 (30) to the stack.

30
8
  
StackExpression

- * 3 + 3 7 / ^ 4 2 2

The next character scanned is "-", which is an operator, so pop its two operands from the stack. Pop 8 from the stack for the left operand and then pop 30 from the stack to make the right operand.

 
 30 - 8 = 22 
StackExpression

Next, push the result of 30 - 8 (22) to the stack.

22
  
StackExpression

Since we are done scanning characters, the remaining element in the stack (22) becomes the result of the prefix evaluation.

prefix notation: - * 3 + 3 7 / ^ 4 2 2
Result: 22

Adjust Calculator Width:

Move the slider to left and right to adjust the calculator width. Note that the Help and Tools panel will be hidden when the calculator is too wide to fit both on the screen. Moving the slider to the left will bring the instructions and tools panel back into view.

Also note that some calculators will reformat to accommodate the screen size as you make the calculator wider or narrower. If the calculator is narrow, columns of entry rows will be converted to a vertical entry form, whereas a wider calculator will display columns of entry rows, and the entry fields will be smaller in size ... since they will not need to be "thumb friendly".

Show/Hide Popup Keypads:

Select Show or Hide to show or hide the popup keypad icons located next to numeric entry fields. These are generally only needed for mobile devices that don't have decimal points in their numeric keypads. So if you are on a desktop, you may find the calculator to be more user-friendly and less cluttered without them.

Stick/Unstick Tools:

Select Stick or Unstick to stick or unstick the help and tools panel. Selecting "Stick" will keep the panel in view while scrolling the calculator vertically. If you find that annoying, select "Unstick" to keep the panel in a stationary position.

If the tools panel becomes "Unstuck" on its own, try clicking "Unstick" and then "Stick" to re-stick the panel.