Page 398 - Ai_417_V3.0_C9_Flipbook
P. 398

Step 3   if Num is divisible by 2 then        Flowchart         START
                            Display "Num is EVEN"

               Step 4   else
                            Display "Num is ODD"
                                                                              Input N1
               Step 5   Stop





                                                                                  Is            No
                                                                            N1 % 2==0?
              Source Code:

              Num=int(input("Enter a Number:"))
                                                                                              Display "Num is ODD"
              if Num%2==0:
                                                                                   Yes
                  print(Num," is EVEN")
              else:
                                                                       Display "Num is EVEN"
                  print(Num," is ODD")
              Output:

              Enter a number: 23
              23 is ODD                                                         STOP

              The if…elif…else Statement
              When there are multiple conditions in a program then it is handled by using elif statement. It means if the
              previous condition is not 'True' then try this condition written next to elif. If one condition is 'True' then the
              statements below it will be executed and the rest of the conditions given using elif will be ignored. In case all
              the conditions written with elif are not 'True', then the statements written just below the else statement will
              be executed. The syntax of the if…elif…else statement is as follows:

              if (condition1):
                Statements 1
              elif (condition2):
                Statements 2
              elif (condition3):
                Statements 3
              ...
              else:
                Statements executed when all above conditions are False
              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 7:  To input two numbers and an operator from the user. Display the result calculated based on the
                operator entered.

              Algorithm
               Step 1   Start



                    396     Touchpad Artificial Intelligence (Ver. 3.0)-IX
   393   394   395   396   397   398   399   400   401   402   403