Page 48 - Modular_V2.0_C++_Flikpbook
P. 48

// Statements executed if condition2 is false

                           }
                       }
                  Program 4: To check if the number is even or odd.

                  #include<iostream.h>
                  #include<conio.h>

                  void main()                                                    DOSBox 0.74, Cpu speed: max 100% cycles,
                  {                                                           Enter a number: 12
                      clrscr();                                               The number is positive.
                                                                              It is an even number.
                      int num;
                      cout<< "Enter a number: ";
                                                                                 DOSBox 0.74, Cpu speed: max 100% cycles,
                      cin>> num;                                              Enter a number: 7

                      if(num > 0)                                             The number is positive.
                      {                                                       It is an odd number.
                          cout<< "The number is positive." << endl;
                          if(num % 2 == 0)

                          {
                              cout<< "It is an even number." << endl;
                          }
                          else
                          {
                              cout<< "It is an odd number." << endl;

                          }
                      }
                      getch();
                  }

                     THE IF…ELSE…IF LADDER                                            True
                                                                          Expression 1        Statement 1
                  The if…else…if ladder is a conditional control
                  structure used to test multiple conditions in a
                                                                               False
                  top-down  approach.  The  program  evaluates
                  each  condition  one  by  one.  As  soon  as  a                     True
                                                                         Expression 2         Statement 2
                  condition is true, the corresponding statement
                  executes,  and  the  rest  of  the  conditions  are           False
                  ignored. If none of the conditions are true, the
                                                                                      True
                  final  else  statement  executes.  The  syntax  of                          Statement 3
                                                                         Expression 3
                  if...else…if ladder is shown below:
                       if(expression1)                                            False      Body of else
                       {
                                                                                                       Statement just
                           statement1;
                                                                                                    below if...else...if ladder
                   46
                          Touchpad MODULAR (Ver. 2.0)
   43   44   45   46   47   48   49   50   51   52   53