You are on page 1of 9

LAB #01:

Introduction to C++
Name of Student: ABU BAKAR

Roll No: 190998

Section: BE-MTS-F-19-A

Student’s Signature: ____________

Date: 11/02/2020

Name of Instructor: EE Engr FAISAL NAWAZ

Instructor’s Signature: __________


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:
Part 1:
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.
Part 2:

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

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.

Functions:
• The 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: ” <<endl;
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”
Task:-

2. Write a program in C++ that takes input from user and display message on screen?
Task:-
Output:

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

Ans.
It is used in C++ in order to include the header file “iostream” in the program. Iostream is
used for calling the commonly used functions like cout,cin in a C++ program. It is used to include
the header file “conio” in a program
2. If we print “Enter an Integer” but cin in a variable of float data type, will it cause any
error?

Ans.
It doesnt causes any error . However it simply displays the integer value entered.

3. What is the only function all C++ programs must contain?


Ans.
The “main( )” is the only function all C++ programs must contain.

You might also like