The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+15 votes
678 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 Boss (19.1k points)
edited by | 678 views
0
Code to evaluate postfix expression

2 Answers

+21 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 Active (3.5k points)
edited by
+2 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 Loyal (6.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

36,171 questions
43,624 answers
124,024 comments
42,893 users