Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 140 - CoreGPT(Podar)_Flipbook_C8
P. 140

    Program 1: To swap the values of two variables using third variable.

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

                      void main()
                      {
                          clrscr();

                          int a, b, temp;
                          a = 10;
                          b = 20;

                           cout<<"Values of a and b before swapping are "<<a<< " and " <<b<<
                            ".\n";
                          temp = a;
                          a = b;

                          b = temp;
                           cout<<"Values of a and b after swapping are "<<a<< " and " <<b<<
                            ".\n";
                          getch();
                      }

                      The output of the above program is displayed as follows:

                                     Values of a and b before swapping are 10 and 20.
                                     Values of a and b after swapping are 20 and 10.

                      Constants

                      A constant is a fixed value which does not change its contents during the program execution. In
                      C++, constants can be divided as follows:
                          Integer Constants: An integer constant is a numeric value without any fractions. For example:
                          10, -45, 35, 56, etc.
                          Floating-Point Constants: Any real number in which a fraction is denoted by a decimal
                          symbol. For example: 345.64, -867.22, 0.8763, etc.

                          String Constants: A string value is a collection of one or more characters written in single or
                          double quotes. For example: 'A', "Hello", "Welcome to C++", etc.
                      Delimiters

                      C++ allows some special characters in its coding called delimiters or punctuators. These are:

                      { } ( ) , ; =
                          Braces { }: These are used to specify the start and end of a block of code like main( ) function.
                          Parentheses ( ): These are used with function names to enclose parameters.

                          Comma ( , ): It is used to separate two variable declarations in same line.

                    138     Premium Edition-VIII

| 135 136 137 138 139 140 141 142 143 144 145 |