Computer Programming - I (MCT-143) Name: - Reg. No.: 2013-MC - Lab Session 2
Computer Programming - I (MCT-143) Name: - Reg. No.: 2013-MC - Lab Session 2
How to display quotation mark “ at output window. (As you should remind that message
between quotation marks was displayed and quotation mark itself was not display)
To counter for these kind of issues escape sequences are used. Below is the list of escape
sequences with description.
\n is used for new line. It performs the same function as of endl but this must be used within
quotation marks contrary to endl. Check the output of following program:
#include <iostream.h>
int main ()
return 0;
1
To create a tab distance \t is used.
Compare the difference between space and tab executing following lines:
Write down output and see it carefully to differentiate space and tab.
Similarly \\ is used to display single backslash \. Test the output of following line:
Task 1
Use \” to display following message at the output window using only one cout command.
Task 2
Display following box using keyboard characters at the output window using only one cout
command.
-----
| |
| |
-----
Task 3
Display the message of Task1 inside the box using only one cout command.
Task 4
Display diamond shown below using only one cout command.
____
/\__/\
/_/ \_\
\ \__/ /
\/__\/
2
Variables:
In C++ different variables can be defined like in elementary algebra. But in case of C++ we need to
define data type of the variables. Data type specifies the kind of data that can be assigned to that
variable.
Two most commonly used data types are int and float.
int x;
In above line we have declared a variable with name x and it is uninitialized variable i.e. it does not
contain any value. We can assign value to it in next instructions but only int data type values can be
assigned to it.
int y = 234;
The above line show that we have declared a variable with name y and it contains value 234. This is
known as initialized variable. The value of variable y can changed in next instructions.
float l,m,n;
In above line we have declared three variables l, m and n with data type float and all three are
uninitialized.
In elementary algebra you have seen that variables are named using single alphabet. In C++ variable
name may contain more than one alphabet following the rules described below:
3
There are some reserved words known as keywords that cannot be used as name of variables even
though they are according to the rules mention above. These keywords are listed at Appendix B of
your text book at page 860.
Example:
#include <iostream.h>
int main ()
{
int number1=468;
cout << “The value stored in variable number1 is:” << number1;
return 0;
}
#include <iostream.h>
int main ()
{
int number1=468;
number1=100;
cout << “The value stored in variable number1 is:” << number1;
return 0;
}
We can use many mathematical operation on the variables declared. For example if you want to
display the value equal to 2 times the value stored in variable, it can be done as:
#include <iostream.h>
int main ()
{
int number1=468;
int number2;
cout << “The value stored in variable number1 is:” << number1;
number2=2*number1;
cout << “\nDouble the value stored in number1 is:” << number2;
return 0;
}
4
It can also be done like:
#include <iostream.h>
int main ()
{
int number1=468;
cout << “The value stored in variable number1 is:” << number1;
cout << “\nDouble the value stored in number1 is:” << number1*2;
return 0;
}
There is an instruction cin that is used to take data from user using keyboard. Its syntax is like:
cin >> nameofvariable
So you have to specify a variable after the cin instruction and whatever the value a user enters
from keyboard will be stored in that variable.
Task 6:
Write a program that takes two values as input and shows the product of them at output. Sample
output is:
Enter 1st number: 2.3
Enter 2nd number: 1.5
The product of two is: 3.45