You are on page 1of 6

Name of Student: _________________________________ Section: _____________________

Student’s Signature: _______________________________Date: ________________________


Name of Instructor: _______________________________ Instructor’s Signature: __________

LAB #01:

Introduction to C++

Lab Objective:

a. To learn how to write algorithms in order to solve difficult problems


b. To learn what does the compiler do
c. To learn how to write a C++ program
d. To learn how to take input and how to output data using C++.

Lab Description:

Computer is organized in different units in which the basic units are input, output,
memory and CPU. Input unit provide data and instructions to the CPU. Memory stores the data
and instructions; CPU executes the instructions and pass the results of the execution to the
output.
Algorithm is a well-define and ordered set of instructions which lead to a solution within finite
number of steps.
Compiler is a program that translates high-level language to machine language and
creates an executable file.
C++ program has one function main () by default without which the program will not
execute as the execution starts from the first instruction of the main function.
In C++ taking input from user and printing output on display of computer is done by including a
header file in the C++ code called iostream.h which notifies the preprocessor to include the
input/output stream contents.

Example:

Let us look at the various parts of the above program −

 The C++ language defines several headers, which contain information that is either
necessary or useful to your program. For this program, the header <iostream> is needed..
This contains pre-defined input/output functions that we can use in our program.

 The line using namespace std; tells the compiler to use the std namespace. Namespaces
are a relatively recent addition to C++.

 The next line '// main() is where program execution begins.' is a single-line comment
available in C++. Single-line comments begin with // and stop at the end of the line.

 The line intmain() is the main function where program execution begins.

 The next line cout<< "Hello World"; causes the message "Hello World" to be displayed
on the screen.

 The next line return 0; terminates main( )function and causes it to return the value 0 to
the calling process.

Some rules for printing,

Description Command Output

Printing double quotes (“) cout<<”Hello \”World\””; Hello “World”

Printing back slash ( \ ) cout<<”Hello \\World”; Hello \ World

Printing tab space cout<<”Hello \t World”; Hello World


jump to new line cout<<”Hello \nWorld”; Hello

World

jump to new line cout<<”Hello” Hello

cout<<endl<<”World”; World

Arithmetic Operations:

Description Operator Answer

Addition cout<<5+2 7

Subtraction cout<<5-2 3

Multiplication cout<<5*2 10

Integer Division cout<<5/2 2

Modulus ( remainder ) cout<<5%2; 1

Division including float Cout<<5.0/2 2.5

Variables:

Variables are used to store data inside them.The name of a variable can be composed of letters,
digits, and the underscore character. It must begin with either a letter or an underscore. Upper
and lowercase letters are distinct because C++ is case-sensitive

Variable type (Data type) How do define in C++

Integer int w;

Decimal float x;

A Single Character char y;

Boolean bool z;
Assignment operator:

Solve expression on right side and assign the result in variable of left side. For example.

#include<iostream>
using namespace std;

void main ()
{
int x, y;
cout<<”Enter a number: ”
cin>>x;
y = x*x;
cout<<“The square of “ << x << “ is: “ << y;
}

LAB TASKS:

1. Write a program that displays the following output on screen


Danny asked, “Are you alright?”
Harry replied.”I’m fine”
2. Write a program in C++ that takes input from user and display message on screen?

THINK!!

1. What is the role of header file iostream.h?


2. If we print “Enter an Integer” but cin in a variable of float data type, will it cause any
error?
3. What is the only function all C++ programs must contain?

You might also like