The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+9 votes
492 views

Consider the following C program: 

  #include <stdio.h>
           #define EOF -1
           void push (int); /* push the argument on the stack */
           int pop  (void); /* pop the top of the stack */
           void flagError ();
           int main ()
          {         int c, m, n, r;
                     while ((c = getchar ()) != EOF)
                    { if  (isdigit (c) )
                               push (c);
                     else if ((c == '+') || (c == '*'))
                    {          m = pop ();
                                n = pop ();
                                r = (c == '+') ? n + m : n*m;
                                push (r);
                      }
                      else if (c != ' ')
                               flagError ();
             }
              printf("% c", pop ());
}


What is the output of the program for the following input?
5 2 * 3 3 2 + * +

  1. 15
  2. 25
  3. 30
  4. 150
asked in DS by Veteran (21.8k points)
recategorized by | 492 views
Code to evaluate postfix expression

2 Answers

+19 votes
Best answer
B) 25
let first part
5 ----push
2------push
push------5*2=10. (pops 5 and 2)

push 3
push 3
push 2
push 3+2 = 5 (pops 2 and 3)
push 5*3 = 15 (pops (5 and 3)
push 15 + 10 = 25 (pops (15 and 10)
answered by Loyal (3.3k points)
selected by
0 votes

It is equivalent to (5*2) + 3 * (3+2) = 10 + 3 *5 = 25

refer https://gateoverflow.in/8408/gate2015-3_12 this method 

answered by Boss (7.5k points)
edited by


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

29,997 questions
37,681 answers
96,745 comments
35,329 users