Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Computer Programming – I (MCT-143)

Name: ____________________ Reg. No. : 2013-MC-___


Lab Session 2
Escape Sequences:
Keeping in mind cout command to display message at output window think about:

 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:

//Testing to display message in two lines using \n

#include <iostream.h>

int main ()

cout << “I am leraning \nComputer Programming”;

return 0;

1
To create a tab distance \t is used.

Compare the difference between space and tab executing following lines:

cout << “Name: Ali \nClass: First Year”<< endl;

cout << “Name:\tAli \nClass:\tFirst Year”;

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:

cout << “1\\2”;

Task 1
Use \” to display following message at the output window using only one cout command.

My name is “Ali” (You will use your name)

I am learning “Computer Programming”

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Integer values from -2,147,483,647 to +2,147,483,647

floatFloating values from -3.4x10-38 to +3.4x1038

How variables are declared in C++:

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.

Rules of naming a variable:

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:

a. First character must be from alphabets a-z or A-Z or underscore (_).


b. After first character you can use alphabets a-z or A-Z or underscore or digits 0-9.
c. The names of variables are case sensitive.

Below are examples of legal variables:


Amount
Total_Amount
_Registration
Session2013

Below are examples of illegal variables:


2013session
#STUDENTS

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;
}

The output of the above program is:

The value stored in variable number1 is:468

Now check the following program:

#include <iostream.h>
int main ()
{
int number1=468;
number1=100;
cout << “The value stored in variable number1 is:” << number1;
return 0;
}

Without using CodeBlocks what you expect the output?

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;
}

Taking input from user:


In above example double of the value stored in a variable is shown at output. What if you want that
user of the application should give value of his own choice and he should get the double the value
at output.

One sample of the application is:


Enter any value you want: 12.3
Double of the value you entered is: 24.6

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.

So the above task can be done like:


#include <iostream.h>
int main ()
{
float number1,number2;
cout << “Enter any value you want: ”;
cin >> number1;
number2=2*number1;
cout << “Double of the value you entered is: ” << number2;
return 0;
}
Task 5:
Modify above program such that it shows both values at the output i.e. the value user entered and
double of that value. Sample output is:
Enter any value you want: 2.6
You Entered: 2.6 Double of which is: 5.2

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

You might also like