Page 253 - Computer_Science_F5
P. 253

Computer Science  Output:






          FOR ONLINE READING ONLY



                                     Figure 4.24: Output of the program




              Activity 4.3:                             Exercise 4.8

             Data type and variable in a program     1.  What is the advantage of data type
            Examine the following C++ program,           in a variable declaration?
            and then answer the questions that       2.  Give the output of the following
            follow:                                      code fragment:

              #include<iostream>                         int x;
              using namespace std;                       x = ‘B’;
              int main()
              {                                          cout << x << ‘\n’;
              char ch;
              cout<<”Enter lower case alphabet: “;   3.  Write a C++ program that reads two
              cin>>ch;                                   numbers entered by the user from
              ch=ch-32;                                  the keyboard, calculates their sum,
               cout<<”\nThe uppercase alphabet is “<<ch;
              cout<<endl;                                and displays the result on the screen.
              return 0;}


            Questions                                Constants
            1.  Type the program code above on       During program execution, a value may
                any C++ text editor program.         change. To avoid this change, you can
            2.  Compile and run the program to       use constants. The constant represents a
                display an output.                   value whose data cannot change during
                                                     program execution. An example of the
            3.  Re-type the program to produce the   constant is Pi = 3.14; you do not need
                same output without using variable   to retype it if you want to use it more
                and data type.                       than once; instead, you can declare it as
            4.  Compare the two programs and         a constant.
                explain how difficult it is to practice
                this program without using variable   The syntax required to declare constant
                and data type.                       is const datatype constantName = value;


                                                 244
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   244                                                     23/07/2024   12:33
   248   249   250   251   252   253   254   255   256   257   258