You are on page 1of 24

Pr

act
ical
Fil
e
Of
BCA607,
R,Pr
ogr
ammi
ng
(
BasedonBCA-
602)
Sessi
on:
(Januar
y-may
,2022)

Submi
tt
edTo: Submi
tt
edBy
:
Mr
s.Sav
itaWadhawan Si
mranj
eetkaur

(
Assi
stant
Prof
essor
) Rol
lno.1319044
Cl
ass:BCA6th(A)

M.
M.I
NSTI
TUTEOFCOMPUTERTECHNOLOGY&BUSI
NESSMANAGEMENT(
MCA)
,

MAHARI
SHIMARKANDESHWAR(
DEEMEDTOBEUNI
VERSI
TY)

MULLANA–AMBALA,
HARYANA(
INDI
A)-133207

(
Est
abl
i
shedunderSect
ion3oft
heUGCAct
,
1956)

(
Accr
edi
tedbyNAACwi
thGr
ade‘
A’
2

I
ndex
1 Wri
teaRpr ogr am toperformf ollowingoper ati
onson
Vector:min( ),max(),mean( ),sqrt()
,length()
,sum( )
,
prod(),sort()(
inascendinganddescendi ngor der)
,
rev()
,addition/subt r
acti
on/ mul ti
pli
cation/div
isionof
twov ectors.
2 Wri
teaRpr ogr am tocreateal istcontainingav ector,a
matri
xandal istandper for
mf oll
owi ngoper ati
ons:
i
. Updat et heelement sint heli
st.
ii
. Mer get wol i
stsintoonel ist.
iii
. Countnumberofobj ectsi nagi venl
ist
.
3 Wr
it
eapr
ogr
am t
ocheckl
eapy
eari
nR.
4 Wr i
teaprogram topri
ntthenumber sfr
om 1t o15in
suchawayt hati
tpri
nts"PUFF"formulti
plesof3,it
pri
nts"CHUG"f ormult
ipl
esof5, andpri
nt" PUFFCHUG"
formulti
plesofbothelseitpr
int
st henumber .
5 Wri
teaprogr
am t
osear
chanumberinali
st.I
fthe
numberi
sfoundt
henpr
inti
tsposi
ti
onel
sepri
nt-1.
6 Createtwolist
stostoreinformati
onoft woSt udent sli
ke
Roll_
No,Name, Age,Subjects.Perf
orm oper at
ionstopr i
nt
twolist
s,addnewcomponentGendert ot wost udents,
AccessRollNoandsubj ect
sofSt udent1,AccessRol l
No
andsubjectsofStudent2.Mer geSubj
ect sintonewLi st
Subject
s_All
.
7 Writeauserdefinedfuncti
ont ogenerateFibonacci seri
es
usingwhil
eloop.
8 Wr
it
eapr
ogr
am i
nRt
omakeasi
mpl
ecal
cul
ator
.

9 Writ
eaRprogram t
ocreat
ean3dimensional
arr
ay
of30el
ementsusi
ngthedim(
)funct
ion.
10 Writ
eaRprogram t
ocreatet
womatri
xandadd,
subt
ract
,mul
ti
plyanddiv
idethemat
ri
ces.
3

Pr
act
ical
1
Aim-Wri
teaRpr ogram toperfor
mf oll
owi ngoper ati
onsonVect or:
min(
),max (
),mean(),sqrt
(),l
ength()
,sum( ),prod(),sort
()(
inascending
and descending order), r
ev(), addit
ion/
subt
ract
ion/mult
ipl
icati
on/div
isi
onoft wov ectors.
.Sour
ceCode
v
1<-c(
5,2,
1,
7,
6)
v
2<-c(
14,
10,
8,
6,
9)
pr
int
("
Mini
mum el
ementi
nvect
orv
1
i
s")pr
int
(mi
n(v
1))
pr
int
("
Mini
mum el
ementi
nvect
orv
2
i
s")pr
int
(mi
n(v
2))
pr
int
("
Maxi
mum el
ementi
nvect
orv
1
i
s")pr
int
(max(
v1)
)
pr
int
("
Maxi
mum el
ementi
nvect
orv
2
i
s")pr
int
(max(
v2)
)
pr
int
("
Meanofv
ect
or
v
1")pr
int
(mean(
v1)
)
pr
int
("
Meanofv
ect
or
v
2")pr
int
(mean(
v2)
)
pr
int
("
Sum ofv
ect
orv
1")
pr
int
(sum(
v1)
)
pr
int
("
Sum ofv
ect
orv
2")
pr
int
(sum(
v2)
)
pr
int
("
Productofv
ect
or
v
1")
4

pr
int
(pr
od(
v1)
)
pr
int
("
Productofv
ect
or
v
2")pr
int
(pr
od(
v2)
)
pr
int
("
sor
ti
ngofv
1")
pr
int
(sor
t(v
1))
pr
int
("
sor
ti
ngofv
2indecr
easi
ng
or
der
")pr
int
(sor
t(v
2,decr
easi
ng=T)
)
pr
int
("
OderonV2"
)
pr
int
(or
der
(v2)
)
pr
int
("
OderonV2i
ndescendi
ng"
)
pr
int
(or
der
(v2,
decr
easi
ng=T)
)
pr
int
("
Rev
erseofv
ect
orrv
1")
pr
int
(rev
(v1)
)
pr
int
("
Rev
erseofv
ect
orr
v
2")pr
int
(rev
(v2)
)
pr
int
("
Sum ofv
1andv
2
i
s")pr
int
((v
1+v
2))
pr
int
("
Subt
ract
ionofv
1andv
2
i
s")pr
int
((v
1-v
2))
pr
int
("
Product
ionofv
1andv
2
i
s")pr
int
((v
1*v
2))
pr
int
("
Div
isi
onofv
1andv
2
i
s")pr
int
((v
1/v
2))
pr
int
("
Lengt
hofv
ect
or
v
1")pr
int
(l
engt
h(v
1))
pr
int
("
Lengt
hofv
ect
or
v
2")pr
int
(l
engt
h(v
2))
5

Out
put
6

Pr
act
ical
2
Ai
m- Wri
teaRprogrammingcr
eat
eal
i
stcont
aini
ngv
ect
or,
amat
ri
xand
al
istandper
for
mf oll
owi
ng
(
i)Updat
etheel
ementSi
nthe
l
i
st.(
Ii)Mer
get
wol
i
stsi
nto
onel
i
st
(
ii
i)
.countnumberofobj
ectSi
nagi
venl
i
st
CODE:
-
l
i
st_
dat
a<-l
i
st(
c("
HTML"
,"
CSS"
,"
Jav
ascr
pt"
,"
C++"
),mat
ri
x(c(
2,4,
6,
8),
nrow=2)
,l
ist
("
Hel
l
o",
"Hi
")
)

pr
int
("
List
:"
)

pr
int
(l
ist
_dat
a)

pr
int
("
Updat
ethet
hir
del
ementoft
he

l
i
st:
")l
i
st_
dat
a[3]="
Rpr
ogr
ammi
ng"

pr
int
("
Newl
i
st:
")

pr
int
(l
ist
_dat
a)

l
1=l
i
st(
1,2,
3,4,
5)

l
2=l
i
st(
"whi
te"
,"y
ell
ow"
,"pi
nk"
)

pr
int
("
Ori
ginal
li
sts:
")

pr
int
(l
1)

pr
int
(l
2)

pr
int
("
Mer
get
hel
i
stsl
1and

l
2:"
)ml
i
st=c(
l1,
l2)

pr
int
("
Newmer
ged

l
i
st:
")pr
int
(ml
i
st)
7

pr
int
("
Numberofobj
ect
sint
hel
i
st1"
)

pr
int
(l
engt
h(l
1))

pr
int
("
Numberofobj
ect
sint
hel
i
st2"
)

pr
int
(l
engt
h(l
2))

pr
int
("
Numberofobj
ect
sint
hemer
ged

l
i
st"
)pr
int
(l
engt
h(ml
i
st)
)

Out
put
8
9
10

Pr
act
ical
3
Ai
m-Wr
it
eapr
ogr
am t
ocheckl
eapy
eari
nR.
CODE:
-
#i
nputy
eari
sal
eapy
earornot
y
ear=as.
int
eger
(readl
i
ne(
prompt
="Ent
eray
ear
:")
)
i
f((
y ear%%4)==0){
i
f((year%%100)==0){
i
f( (
year%%400)==0)
{pr i
nt(
paste(year,
"i
saleapyear"
))
}else{
print(
paste(year,
"i
snotaleapyear"
))
}
}else{
print(
paste(year,
"i
saleapyear"
))
}
}else{
print(
paste(year,
"i
snotaleapyear"
))
}

Out
put
11

Pr
act
ical
4
Ai
m-Wr
it
eapr
ogr
am t
opr
intt
henumber
sfr
om 1t
o15i
nsuchaway
thati
tprints"
PUFF"formul
ti
plesof3,i
tpr
int
s"CHUG"formulti
plesof5,
andprint"PUFFCHUG"formul
tipl
esofbot
helsei
tpri
ntsthenumber.
CODE: -
for(
iin1:15)
{
if(
(i%%3==0)&&( i%%5==0)
)
{pri
nt("
PUFFCHUG"
)

}
el
seif(i
%%3==0)
{
pr
int
("
PUFF"
)

}
el
seif(i
%%5==0)
{
pr
int
("
CHUG"
)

}
el
se
pri
nt(
i)
}
Output
12
13

Pr
act
ical
5
Aim-Writ
eaprogram t
osearchanumberi
nal
i
st.I
fthenumberi
sfound
thenpr
inti
tsposi
ti
onelsepr
int-
1.
CODE: -
l
<-list(20,30,45,54,56,
76,88,
90)
print("
listis")
print(l
)
num<- r
eadl i
ne("enterthenmubertobe
sear ched")num<- as.i
nteger
(num)
found<-
FALSEPosi t
io
n<-1
for( ii
n1: 8)
{
if
( l[
i]
==num)
{
found<- TRUE
Posi ti
on<-i
cat("positionoft henumberi s",
i
)br eak
}
}
i
f( found==FALSE)
{
print("numbernot
found" )cat (
"poost i
onis",
Posi ti
on)
}
14

Out
put
15

Pr
act
ical
6
Aim-Createtwoli
ststostorei
nformat i
onoftwoSt udentsl
ikeRoll
_No,
Name, Age,Subj
ects.Per
form operat
ionstopri
nttwol i
sts,addnew
componentGendert otwostudents,AccessRol
lNoandsubj ect
sof
Student1,
AccessRollNoandsubjectsofStudent2.Mer geSubject
s
i
ntonewLi stSubj
ects_Al
l.

print
("Student 1i nfo")
student1<-l ist(roll
no=1319107, name=" Nraj"
,age=21, subj
ects=c(
"C"
,"
c++"
))
print
(student 1)
#Cr eateal istofst udenti nf ormat ionwi thoutnamed
component sst udent 2<-l ist (1320132, "j
igar",19,c(
"R",
"PHP"))
#Usenames( )t ogi venamest ocomponent sof
l
istnames( student 2)<-c( "
rol lno","
name" ,"age",
"subjects")pr int("Student 2i nf o")
print
(student
2)#Add
Gender
print
("2st udent sinf oaf ter
addinggender "
)st udent 1$Gender<-
"Male"st udent 2["Gender "]<-" Mal e"
print
(student 1)
print
(student 2)
#$oper at orisusedt oext ractel ement sofLi stbyli
ter
al
names.subj ect s_1<-st udent 1[ [
"subjects"]]
cat("
Student 1'ssubj ects" , subj ects_1,"\
n" )
subjects_ 2<-st udent 2$subj ect s
cat("
Student 2'ssubj ects" , subj ects_2,"\
n" )
subjects_ all<-append( subj ect s_1,
subjects_ 2)cat ("
All subject s" ,
subjects_ all)
16

out
put
17

Pr
act
ical
7

7.Wr
it
eauserdef
inedf
unct
iont
ogener
ateFi
bonacci
ser
iesusi
ngwhi
l
e
l
oop.
CODE: -
#t akei nputf r
om t heuser
nterms=as. i
nteger (
readl
ine(prompt ="
Howmany
terms?" ))#firsttwot er
ms
n1=0
n2=1
count=2
#checki fthenumberoft ermsi sv al
id
i
f(nt erms<=0){
print (
"Pl eseent eraposi ti
vei nteger"
)
}else{
if
(nt erms==1)
{pr i
nt( "
Fibonacci
sequence: "
)pr int(n1)
}el se{
pr int("Fibonacci sequence:
")pr int (
n1)
pr int(n2)
whi l
e( count<nt erms)
{nt h=n1+n2
print(nth)
#updat e
valuesn1=
n2
n2=nt h
count=count+1
}
}
}
18

Out
put
19

Pr
act
ical
8

Ai
m-Wr
it
eapr
ogr
am i
nRt
omakeasi
mpl
ecal
cul
ator
.
CODE: -
#Pr ogr am makeasi mplecal culat
ort hatcanadd, subt
ract
,mult
ipl
yanddivi
de
usingf unctions
add<-f unction(x, y)
{r eturn(x+y )
}
subt ract<-f unct ion( x,y)
{r eturn(x-y )
}
mul tiply<-f unct ion(x, y)
{r eturn(x*y )
}
divide<-f unct i
on( x,y )
{r eturn(x/y )
}
#t akei nputf rom t he
userpr int("
Sel ect
oper ation. "
)
print("1.Add" )
print("2.Subt ract")
print("3.Mul ti
pl y
" )
print("4.Divide" )
choi ce=as. integer (readline(prompt ="Enter
choi ce[1/ 2/3/4]: ")
)num1=
as.integer (readline( prompt ="Enterfirstnumber:"))num2=
as.integer (readline( prompt ="Entersecondnumber :"
))
oper ator<-swi t
ch( choi ce,"
+",
"-",
"*"
,"
/")
resul t<-swi tch( choi ce,add(num1, num2) ,subt
ract(num1,num2),mul
ti
ply
(num1,
num2) , divi
de( num1, num2) )
print(past e(num1, oper ator,
num2, "=",r
esult
))
20

out
put
21

Pr
act
ical
9
Aim-Wri
teaRpr
ogram t
ocr
eat
ean3di
mensi
onal
arr
ayof30el
ement
s
usi
ngthedi
m()f
uncti
on.
CODE: -
#Cr eatet wo
vect orsdat a1<-
c(1,2,3,4,5,
6)
dat a2<-c( 60,18, 12, 13, 14,19)
#assi gningrownames
row. names=c( " r
ow1" ,
"r
ow2" ,"
row3
")#assi gningcol umnnames
column. names=c( "
col 1"
,"col
2",
"col
3")
#assi gningarray names
mat r
ix.names=c( '
array1','
arr
ay2',
'
arr
a
y3')#passt hesev ectorsasi nputto
thear r
ay .#3r ows, 3col umnsand3
array s
resul t<-array(c(data1, data2),di
m =c(3,
3,
3),
dimnames=l i
st(row. names, column.
names,matr
ix.
names)
)
print(result)

out
put
22
23

Pr
act
ical
10
Ai
m-Wr i
teaRprogram t
ocr
eatetwomat
ri
xandadd,
subt
ract
,
mul
ti
plyanddi
vi
detheematr
ices.
CODE: -
#Cr eatetwo2x3mat rixes.
m1=mat ri
x(c(1, 2,
3,4, 5,6)
,nrow=
2)pr int("
Mat ri
x-1:")
print(m1)
m2=mat ri
x(c(0, 1,
2,3, 0,2)
,nrow=
2)pr int("
Mat ri
x-2:")
print(m2)
resul t=m1+m2
print("Resultofaddi ti
on")
print(result
)
resul t=m1-m2
print("Result
ofsubt racti
on")
print(result
)
resul t=m1*m2
print("Result
ofmul t
ipl
icati
on" )pri
nt(resul
t)
resul t=m1/m2
print("Resultof
division:"
)pr i
nt(result
)
24

out
put

You might also like