You are on page 1of 2

Worksheet 1

firstnum = 20; // assign any value of variable firstnum
secondnum = 10; // assign any value of variable secondnum
result = firstnum + secondnum; // get the sum of two (2) number
cout << "\n" << result;
result = firstnum - secondnum;
cout << "\n" << result;
result = firstnum * secondnum;
cout << "\n" << result;
result = firstnum / secondnum;
cout << "\n" << result;

Worksheet 3

#include <iostream>
using namespace std;
int main ()
{
int firstnum, secondnum;
cout << "enter the first number: ";
cin >> firstnum;
cout << "enter the second number: ";
cin >> secondnum;
cout << “The sum of “ << firstnum << “ and “ << secondnum << “ is: ” << firstnum + secondnum << endl;
cout << “The difference of “ << firstnum << “ and “ << secondnum << “ is: ” << firstnum - secondnum << endl;
cout << “The product of “ << firstnum << “ and “ << secondnum << “ is: ” << firstnum * secondnum << endl;
cout << “The quotient of “ << firstnum << “ and “ << secondnum << “ is: ” << firstnum / secondnum;
return 0;
}

Worksheet 4

1. C++ was the object-oriented language for a while, is still heavily used, and has inspired other highly popular object-oriented
languages such as C# and Java. C++ has dominated the “system programming” market and changed how many of us think
about programming from completely procedural to object-oriented thinking.
2. True, the C++ compiler produces an object code which is an executable file from a source code.
3. The principles of OOP (Object-Oriented Programming) are:
a. Encapsulation - which is a programming mechanism that binds code and the data it manipulate together, and keeps
both safe from outside interference and misuse.
b. Polymorphism - which helps reduce complexity by allowing the same interface to specify a general class of action.
c. Inheritance - which is the process by which one object can acquire the properties of another object.
4. C++ programs begin execution in the main() function.
5. Header file stands for input and output stream used to take input with the help of “cin” and “cout” respectively. There are two
types of header file:
a. Pre-existing header files - which are already available in C++ compiler.
b. User-defined header files - which are defined by the user and can be imported using “#include”.
6. iostream stands for standard input-output stream. This header file contains definitions to objects like cin, cout, cerr etc.
7. A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc.)
inside it. They are used as additional information to differentiate similar functions, classes, variables, etc. with the same name
available in different libraries.
8. A variable is a name given to a memory location. It is the basic unit of storage in a program.
9. d. and e.
10. You can create a single-line comment by typing //. You can create a multi-line comment by starting with /* and ending with
*/.
11. The correct syntax for if statement in C++ is: if (condition) {

//body of if statement

The correct syntax for for loop in C++ is: for (initialization; condition; update) {
// body of for loop
}
12. #include <iostream>
using namespace std;
int main () {
     int x;
     int y;
     double z;
     x = 3;
     y = 5;
     z = 1.6;
     cout << x << endl << y << endl << z;
     return 0;
}
13. #include <iostream>
using namespace std;
int main () {
     int num1, num2, num3, num4, num5, average;
     cout << "enter first number: ";
     cin >> num1;
     cout << "enter second number: ";
     cin >> num2;
     cout << "enter third number: ";
     cin >> num3;
     cout << "enter fourth number: ";
     cin >> num4;
     cout << "enter fifth number: ";
     cin >> num5;
     average = (num1 + num2 + num3 + num4 + num5) / 5;
     cout << "The average is: " << average;
     return 0;
}
14. keyboard
15. Bjarne Stroustrop

You might also like