Page 18 - Modular_V1.1_Flipbook
P. 18

For example:

                  int age;
                  char grade;
                  Where, int and char are the data types, and, age and name are the variable names. You can also
                  declare multiple variables of the same type in a single line in the following way:

                  int emp_code, age, pincode;
                  Initializing a Variable

                  Initializing a variable means to assign a value to the variable. You can assign a value to a variable
                  using the assignment operator (‘=’) in the following way:

                  age = 21;                    // initializing an integer variable
                  grade = ‘A+’;                // initializing a character variable using single quote
                  In the preceding example, you can see that the single quotes are used to initialize a character

                  variable. A variable can also be initialized during their declaration itself in the following way:
                  int age = 21;
                  char grade = ‘A+’;
                  Let us create a program to better understand the concepts.

                  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<< “.”;
                  getch();
                  }
                  The output of the preceding program is as shown:











                  16      Touchpad MODULAR (Version 1.1)-X
   13   14   15   16   17   18   19   20   21   22   23