Page 283 - Computer_Science_F5
P. 283

Computer Science       Exercise 4.13                  4.  Write a C++ program to
                                                                 display even numbers, odd
                                                                 numbers, or prime numbers
             1.  Write  a C++ program that displays the
                                                                 which lie within a range
                Pascal triangle, as shown in Figure 4.52,
                                                                 entered by the user.
          FOR ONLINE READING ONLY
                when a user enters the power of expansion.
                                                             5.  What is printed by the
                                                                 following code fragment?
                                                                   int x = 0;
                                                                   do {
                                                                   cout << x << “ “;
                                                                   x++;
                                                                   } while (x < 10);
                                                                   cout << ‘\n’;

                   Figure 4.52: Output of the program

             2.  Use for loop  to write  a C++ program                Functions
                which outputs the multiplication table as
                shown in Figure 4.53:                       Concept of functions
                                                            In programming, you can
                                                            sometimes    find   the   same
                                                            instruction is applied repeatedly
                                                            throughout the main program.
                                                            The instruction does not iterate
                                                            the  solution  according  to the
                                                            condition like what is happening
                                                            within the loop. Still, it is used
                                                            repeatedly through the program
                                                            according  to  the  user  tasks. To
                                                            avoid retyping such instructions
                   Figure 4.53: Output of the program
                                                            many times within the main
             3.  Create a simple calculator using C++       program, you can use a function.
                programming language to solve the           A function  is a block  of code
                factorial of a number and read an exponent   in a program which run only
                when the user enters the base and power of   when it is called. For example,
                a particular exponent. A calculator should   a program has a block of code
                                                            that calculates interest of money
                allow the user to choose either to solve    deposited by one customer in a
                factorial or exponent of the number using   bank. We do not want to rewrite
                a switch case statement.                    the same instructions, if we wish


                                                 274
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   274                                                     23/07/2024   12:34
   278   279   280   281   282   283   284   285   286   287   288