You are on page 1of 14

LahoreGarri

sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er

Di
git
alFor
ensi
cResear
chandSer
viceCent
er

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ng
Cl
ass:BS-DFCS
LabTask2-Var
iabl
es,Dat
aTypes,I
nput
,Out
put

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page1
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er

LabTask2-Var
iabl
es,Dat
aTypes,I
nput
,Out
put
I
nst
ruct
ions

Compl et
ethetasksbelow andsavet
hem oncomput er
.Inser
tthesoluti
on/answer
i
nt hi
sdocumentasdi rectedbel
ow.Youmustshow theexecuti
onofbel ow tasks,
al
ongwi thyourcompl etedWorddocumenttogetyourwor kgraded.Youmust
al
sosubmi tt
hisWor ddocumentonthel
inkpr
ovided.Incaseofanypr oblemswi t
h
submissi
onont hel
ink.

Del
i
ver
abl
es

Compi
leasingl
eWor ddocumentbyf
il
li
ngi
nthesol
uti
on/
answerpar
tandsubmi
t
t
hisWordf
il
eonl i
nk.

Thi
slabi
sgr
aded.Mi
nmar
ks:0.Maxmar
ks:1
0.

LabTasks

Q1
) Readt
hedescr
ipt
ionsbel
ow car
eful
l
y:

C++Var iables:
I
npr ogrammi ng,avari
abl
eisacont
ainer(st
oragear
ea)t
oholddat
a.
Toindicatet hestor
agearea,eachvariabl
eshouldbegivenauniquename
(i
denti
fi
er )
.Forexample,

i
ntage=1
4;

Here,agei
savar
iabl
eoft
hei
ntdat
atype,andwehaveassi
gnedani
nteger
val
ue14toi
t.

Rul
esf
ornamingavar iabl
e:
 Avari
ablenamecanonl yhaveal phabets,numbers,andtheunder
score
_.
 Avari
ablenamecannotbegi nwithanumber .
 I
tisa pr efer
red pract
ice to begi
n variabl
e nameswi t
hal owercase
char
acter.Forexample,namei sprefer
abl et
oName.
 Avari
ablenamecannotbeakeywor d.Forexampl e,i
nti
sakeywordthat
i
susedt odenoteint
eger s.

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page2
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er
 A vari
able name can st
artwi
th an under
scor
e.However
,it
's not
consi
deredagoodpract
ice.

C++Li t
erals:
Lit
er al
sar edatausedf orrepresent i
ngf i
xedval
ues.Theycanbeuseddirect
ly
i
nt hecode.Forexampl e:1,2.5,'c'etc.
Her e,1,2. 5and' c'ar
el it
erals.Why?Youcannotassi gndif
fer
entval
uesto
theset erms.
Her e'salistofdi
ff
erentli
teral
si nC++pr ogramming.

1.I
ntegers
Anintegerisanumericl
it
eral(
associat
edwit
hnumber s)withoutanyfr
act
ional
orexponential
par
t.Ther
earethreetypesofi
ntegerl
i
teral
sinCpr ogr
amming:

Deci
mal
:0,-9,22et
c

Oct
al:021
,077,033et
c

Hexadeci
mal
:0x7f
,0x2a,0x521et
c

2.Floati
ng-pointLit
eral
s
Afloati
ng-pointli
ter
alisanumer i
cli
ter
alt
hathasei
theraf
ract
ionalf
orm oran
exponentform.Forexampl e:
-2.0 | 0.0000234 | -0.
22E-5

3.Charact
ers
A charact
erli
terali
scr eat
ed byenclosing a si
ngl
e char
act
eri
nsi
de si
ngl
e
quotat
ionmarks.Forexample:'
a','
m','
F','
2','
}'et
c.

4.EscapeSequences
Sometimes,i
tisnecessaryto use charact
erst
hatcannotbe t
yped orhas
speci
almeaning in C++ pr
ogrammi ng.Forexampl
e,newline (
ent
er),t
ab,
quest
ionmark,et
c.

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page3
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er

5.StringLit
eral
s
Ast r
ingli
terali
sasequenceofchar
act
ersencl
osedi
ndoubl
e-quot
emar
ks.
Forexample:

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page4
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er

Q2) Readt
hedescr
ipt
ionbel
ow car
eful
l
y:

C++FundamentalDataTypes:
Thetabl
ebelow showsthefundament
aldat
atypes,t
hei
rmeani
ng,andt
hei
r
si
zes(i
nbyt
es):

1.C++int
Theintkeywordisusedtoindi
cat
eint
egers.
I
tssizeisusuall
y4byt es.Meani
ng,itcanstor
eval
uesf
rom -21
47483648t
o
2147483647.
Forexample,

i
ntsal
ary=85000;

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page5
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er

2.C++f l
oatanddoubl e
fl
oatand doubl e are used t
o storef l
oat
ing-pointnumber s(deci
mals and
exponenti
als).
Thesizeoff l
oatis4bytesandt hesizeofdoubleis8bytes.Hence,doublehas
twoti
mest hepr eci
si
onoff l
oat.Tolearnmore,visi
tC++floatanddouble.
Forexample,

fl
oatar
ea=64.
74;
doubl
evol
ume=134.
64534;

3.C++char
Keywordcharisusedf
orcharact
ers.
I
tssi
zeis1byte.
Charact
ersinC++areencl
osedinsi
desi
ngl
equot
es''
.
Forexample,

chart
est='
h';

4.C++wchar _t
Wi decharact
erwchar _tissi
mil
artothechardatat
ype,excepti
tssi
zeis2
bytesi
nsteadof1 .
I
tisusedt orepresentchar
act
ersthatr
equi
remorememorytorepr
esentt
hem
thanasinglechar.
Forexample,

wchar
_tt
est=L'
‫'ם‬/
/st
ori
ngHebr
ewchar
act
er;

5.C++bool
Thebool datatypehasoneoftwopossibl
eval
ues:tr
ueorfal
se.
Booleansar eusedincondi
ti
onalst
atementsandloops(
whichwewi
l
llear
nin
l
aterchapters)
.
Forexampl e,

bool
cond=f
alse;

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page6
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er
6.C++void
The voi
d keywor
dindi
catesanabsenceofdat a.Itmeans"not
hing"or"
no
val
ue".
Wewi ll
usevoidwhenwelearnaboutfunct
ionsandpoi
nter
s.
Note:Wecannotdecl
arevar
iabl
esofthevoidtype.

C++TypeModi f
ier
s
We can furt
hermodi fysome oft he f
undamentaldatat
ypesbyusi
ng t
ype
modi
fi
ers.Therear e4typemodifi
ersinC++.Theyare:
 si gned
 unsi gned
 shor t
 l ong
Wecanmodi f
ythef ol
l
owingdatatypeswitht
heabovemodifi
ers:
 i nt
 doubl e
 char
C++Modif i
edDat aTypesLi
st:

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page7
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er

Let
'sseeaf
ew exampl
es.

l
ongb=4523232;
l
ongi
ntc=2345342;
l
ongdoubl
ed=233434.
56343;

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page8
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er
shor
td=3434233;/
/Err
or!outofr
ange
unsi
gnedi
nta=-5; //Err
or!canonl
yst
oreposi
ti
venumber
sor0

Q3) Readt
hedescr
ipt
ionbel
ow car
eful
l
y:

C++Output
I
nC++,coutsendsformatt
edoutputt
ostandar
dout
putdevi
ces,suchasthe
scr
een.Weusethecoutobj
ectal
ongwitht
he<<oper
atorf
ordi
splayi
ngout
put.

Exampl
e1:St
ri
ngOut
put

#incl
ude<i
ostream>
usingnamespacest
d;

i
ntmai n(
){
//pri
ntst
hestr
ingencl
osedindoubl
equot
es
cout<<"Thi
sisC++Programming";
ret
urn0;
}

Wr
it
ethi
scodei
nDEVC++andt
esti
tsout
put
.

Example2:NumbersandCharact
ersOutput
Topri
ntthenumbersandcharact
ervar
iables,weuset
hesamecoutobj
ectbut
wit
houtusi
ngquotat
ionmarks.

#incl
ude<i
ostream>
usingnamespacest
d;

i
ntmai n(
){
i
ntnum1=70;
doublenum2=256.
783;
charch='A'
;

cout<<num1<<endl;/ /pr
inti
nteger
cout<<num2<<endl ;//pri
ntdouble
cout<<"char
act
er:"<<ch<<endl ;//pr
intchar
ret
urn0;
DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page9
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er
}

Wr
it
ethi
scodei
nDEVC++andt
esti
tsout
put
.

C++Input
I
n C++,cint
akesfor
matt
ed i
nputfr
om st
andar
dinputdevicessuchast he
keyboar
d.Weuset
hecinobj
ectal
ongwi
ththe>>oper
atorf
ortaki
nginput
.

Exampl
e3:I
ntegerI
nput
/Out
put

#incl
ude<i
ostream>
usingnamespacest
d;

i
ntmai n(){
i
ntnum;
cout<<" Ent
eranint
eger:";
cin>>num; / /Taki
nginput
cout<<" Thenumberis:"<<num;
retur
n0;
}

Wr
it
ethi
scodei
nDEVC++andt
esti
tsout
put
.

C++Taki
ngMul
ti
pleI
nput
s:

#incl
ude<i
ostream>
usingnamespacest
d;

i
ntmai n(
){
chara;
i
ntnum;

cout<<"
Ent
erachar
act
erandani
nteger
:";
ci
n>>a>>num;

cout<<"
Char
act
er:"<<a<<endl
;
cout<<"
Number
:"<<num;

r
etur
n0;
}

Wr
it
ethi
scodei
nDEVC++andt
esti
tsout
put
.
DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page1
0
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er

Q4) Repor
tal
ltheout
put
spr
act
icedi
npr
evi
oust
asks.
Answer
:

Scr
eenshot
s

Q5) Wr i
teapr ogram thatdeclar
esseverali
ntandfl
oattypevari
abl
es,wit
hout
i
nit
iali
zi
ng( st
ori
ngvalue)them,andthenpr i
ntst
hei
rval
ues.Ist
hereanypatt
ern
t
otheval ues?
Writeyourprogram below andanswertheabovequest
ion.

Not
e:
- i
ntvariableareusedtost
orei
ntegervaluesinit,itcanbeinit
ial
i
zedas“i nt
a=1 ;”,wher e‘a’ ist
hevar
iableand‘2’ i sthevaluestor
edi nit
.
- fl
oatvar i
ablesareusedtost
oref ract
ionalvalues,itcanbei niti
ali
zedas
“f l
oatb=1 .1
234”,where‘b’ i sthevariableand‘1 .
1234’ isthefloat
val
uest oredinit
.

Answer
:

Sol
uti
on:Code

Scr
eenshot
s

Q6) Wri
te a pr
ogr
am to comput
e and di
spl
ayt
he vol
ume ofa cyl
i
nderwi
th
f
oll
owi
ngmeasurements.

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page1
1
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er

2
wherevol umev=πr h,π=3. 1 4159
2
Hint
:r = r *r,π=22/ 7.
Notet hatyoumustdef i
net heval ueofπ asanamedconst ant(
i.
e.#defi
ne
macro) .
e.g.
,ifyouwantt odef ineaval uelike1.
1234i nvar
iabl
ex,youcandoi tli
ke
“#def inex1.1234”
Developt helogi
coft hisprogr am thr
oughaf lowchar
tinVisualLogi
candinsert
thefl
owchar tbelow.
Conver tt
helogicint
oaC++pr ogram andinsertt
hecodebel ow.

Answer
:

Fl
owchar
t

Solut
ion
Sol
uti
on:Code

Scr
eenshot
s

Q7) Wr i
teaprogr am t
hataskst
heusertoenterathr
ee-di
gitnumber
,thenpr
int
s
thesum ofitsdi
gits.Asessi
onwit
htheprogram shoul
dhavethefol
l
owing
appearance:
Enterathree-di
gitnumber:128

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page1
2
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er
Thesum is:1
1
Hint
:Ifnisatwo-di
giti
nteger
,thenn%1
0ist
hel
astdi
giti
nnandn/
10i
snwi
tht
he
l
astdigi
tremoved.

Answer
:

Sol
uti
on:Code

Scr
eenshot
s

Q8)Wri
teaprogr
am t
oprompttheusert
oinputt
hei
ntegr
alval
ueofaandpr
int
outt
her
esul
tasshownbelow:

 
Resul
t:
Theval
ueofais:10
……………………. .
Theval
ueof++ais:11
Nowtheval
ueofai s:11
 
Theval
ueofa++is:11
Now t
heval
ueofai s:12
 
Theval
ueof--ais:
11
Now t
heval
ueofai s:
11
 
Theval
ueofa--is:11
Now t
heval
ueofai s:10

Answer
:

Sol
uti
on:Code

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page1
3
LahoreGarri
sonUniver
sit
y
Digi
talFor
ensicResear
chandSer
viceCent
er

Scr
eenshot
s

DFCS31
01:I
ntr
oduct
iont
oPr
ogr
ammi
ngC++Page1
4

You might also like