Name of Student: ..
Roll No.:
Date of Experiment: ..
Report submitted on: ..
Marks obtained:
Remarks:
It is very important that you observe the following rules for the lab usage. These rules
are very important in order to maintain an environment in the lab that is conducive to
learning.
Please dont make noise in the lab. The lab is being used by a lot of students at
the same time. Please make sure that your behavior does not become a source
of inconvenience for anybody else.
Please make sure that you have seated yourself in the lab before it starts.
Attendance is compulsory for all the students and nobody will be allowed to
enter the lab after 15 minutes.
In case you want to leave the lab, raise your hand and seek the permission from
the Instructors present in the lab. Only one student is allowed outside the lab at
a given time
Please turn off your cell phones during the lab. You are not allowed to use your
cell phone during the lab.
Use of internet is not allowed during lab hours unless explicitly instructed by the
instructor
Please follow the deadlines for your lab/assignment submissions. No grades
would be awarded in case of late submission.
Please make sure that you delete your code from the lab PC after the lab is over,
so that it cannot be misused by someone else.
Plagiarism would not be tolerated under any circumstances. You will be given a
zero in such cases and your case would be forwarded to Disciplinary Committee
(DC) for further action.
Listing 1.1
First Program - Hello World
#include<iostream>
Using namespace std;
int main()
{
Cout<<"Hello World";
return 0;
}
#include<iostream>
This tells the compiler to first include standard C++ library named iostream to our
project. IOSTREAM is an example of a header file (sometimes called an include file). Its
concerned with basic input/output operations, and contains declarations that are
needed by the cout identifier and the << operator .Without these declarations, the
compiler wont recognize cout and will think << is being used incorrectly.
int main()
This statement declares the main function. C++ program can contain many functions but
must always have one main function. A function is a self-contained module of code that
can accomplish some task. The "int" specifies the return type of main. In this case, 0 is
returned to the operating system.
{
cout is a function from a standard C++ library that is used to print strings to the standard
output, normally your screen. The compiler links code from these standard libraries to
the code you have written to produce the final executable.
Return 0;
The keyword return is a special statement which is used to exit a function and return a
value. This statement exits the function main indicating its completion and returns the
value 0 to the operating system indicating that the program successfully ran to
completion.
}
This closing bracket denotes the end of the main function.
Cout
On most program environments, the standard output by default is the screen, and the
C++ stream object defined to access it is cout.
For formatted output operations, cout is used together with the insertion operator,
which is written as << (i.e., two "less than" signs).
#include <iostream>
using namespace std;
void main ()
{
int x = 3;
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the value of x on screen
}
Escape Sequences
Character combinations consisting of a backslash (\) followed by a letter or by a
combination of digits are called "escape sequences." To represent a newline character,
single quotation mark, or certain other characters in a character constant, you must use
escape sequences. An escape sequence is regarded as a single character and is therefore
valid as a character constant. Escape sequences are used to format our output.
The following escape sequences can be used to print out special characters.
Escape Sequence Description
\n Newline
\t Horizontal tab
\\ Backslash
#include<iostream>
int main()
{
cout<<"This\nis\na\ntest\n\nShe said, How are you?\n";
return 0;
}
Output
This
is
a
test
She said, How are you?
Listing 1.3
This programs shows the use of Horizontal tab Escape Sequence (\t)
#include<iostream>
int main()
{
Cout<<"This is a test \t She said, How are you?\n";
return 0;
}
Output
This is a test She said, How are you?
The gap between the sentences is due to Horizontal tab escape sequence.
Listing 1.4
This programs shows the use of Newline Escape Sequence (\n) with Double quote
escape sequence (\)
#include<iostream>
int main()
{
cout<<"This is a test \nShe said,\"How are you?\"" ;
return 0;
}
Output:
This is a test
She said, How are you?
The purpose of the above programs is to give you an idea that escape sequences can be
used in any combination to format your output.
Using Directive:
A C++ program can be divided into different namespaces. A namespace is a part of the
program in which certain names are recognized; outside of the namespace theyre
unknown. The directive using namespace std; says that all the program statements that
follow are within the std namespace. Various program components such as cout are
declared within this namespace. If we didnt use the using directive, we would need to
add the std name to many program elements. e.g.
Comments
Comments are an important part of any program. They help the person writing a
program, and anyone else who must read the source file, understand whats going on.
The compiler ignores comments, so they do not add to the file size or execution time of
the executable program. Comments start with a double slash symbol (//)
and terminate at the end of the line. Theres a second comment style available in C++:
/* this is an old-style comment */
Arithmetic Operators:
There are following arithmetic operators supported by C++ language: Assume variable
A holds 10 and variable B holds 20, then:
Show Examples
Listing 1.5:
#include <iostream>
using namespace std;
main()
{
int a = 21;
int b = 10;
int c ;
c = a + b;
cout << "Line 1 - Value of c is :" << c << endl ;
c = a - b;
cout << "Line 2 - Value of c is :" << c << endl ;
c = a * b;
cout << "Line 3 - Value of c is :" << c << endl ;
c = a / b;
cout << "Line 4 - Value of c is :" << c << endl ;
c = a % b;
cout << "Line 5 - Value of c is :" << c << endl ;
c = a++;
cout << "Line 6 - Value of c is :" << c << endl ;
c = a--;
cout << "Line 7 - Value of c is :" << c << endl ;
return 0;
Output:
When the above code is compiled and executed, it produces the following result:
Line 4 - Value of c is :2
Line 5 - Value of c is :1
Line 6 - Value of c is :21
Line 7 - Value of c is :22
Step 1:
First of all click on Start Menu All Programs Program files SoftwareBloodshed
Dev C++
This would launch the following window. Then closed the pop up window.
Step 2:
This would launch the following window. Remember this step is very important so you
have to be careful. Click on the Windows Console Application and then click on the
project name and write Lab1. Then click OK.
Step 3:
After clicking the OK in the step 2. You will see the following window.
#include<iostream>
int main()
{
cout<<"Hello World";
return 0;
}
Step 5:
Now execute the program by clicking on EXECUTECompile
Step 6:
After pressing compile the following window appears.
Your tasks
TASK 1
Write a program to print following information on screen. (Perform this task with cout )
Task 2
Write a program to draw shape of a boat on console.