Page 244 - Ai_V1.0_Class9
P. 244

Where
                 • if, else, elif are keywords.
                 • condition1, condition2 and condition3, …., can be any expression.
                 • statements1, statements2 and statements3 are always indented can be single or a block of statements.

                Program 4:  To input two numbers and an operator from the user. Display the result calculated based on the
                operator entered.

              Algorithm                                                                 Flowchart
               Step 1   Start                                                             START

               Step 2   Input N1, N2, op
               Step 3   if op is same as that of '+' then                              Input N1, N2
                            Display N1+N2
               Step 4   else, if op is same as that of '-' then
                            Display N1-N2                                                  Is     Yes     Display
                                                                                       op == "+"?        N1 + N2
               Step 5   else, if op is same as that of '*' then
                            Display N1*N2                                                    No
               Step 6   else                                                                Is    Yes     Display

                            Display "Sorry! Invalid Operator"                           op == "–"?       N1 – N2
               Step 7   Stop
                                                                                             No
              Source Code:                                                                 Is     Yes

                                                                                       op == "*"?         Display
              N1=int(input("Enter First Number:"))                                                       N1 * N2
              N2=int(input("Enter Second Number:"))                                          No
              op=input("Enter an operator(+, –, * only):")                              Display
                                                                                 "Sorry! Invalid Operator"
              if op=="+":
                  print("The addition is", N1+N2)
                                                                                          STOP
              elif op=="–":
                  print("The subtraction is", N1–N2)

              elif op=="*":
                  print("The multiplication is", N1*N2)

              else:
                  print("Sorry! Invalid Operator")
              Output:

              Enter First Number: 5
              Enter Second Number: 4

              Enter an operator(+,–,* only): *
              The multipication is 20






                    242     Artificial Intelligence Play (Ver 1.0)-IX
   239   240   241   242   243   244   245   246   247   248   249