We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 38
Write a progeann to,
on
(o) lot the 81H OF lg oF « given
nunnber,
(0). Ghook vavothor the qlvon titnbor be
Anostrong or not,
(0) Hrlnt the prttue Huntions trong ton
whore 1 Fs watuga nMinihor given,
Auawor
fy telat the Sn oF Hy OF w iven Number
uneam
Hinclides lostienin =
fag, wamnespinces ate
dnt sanity
'
Int fat sun =
conor tne 4
wl
wteuly
while: (2 t= Oy
{
ree 10;
sun ~ su
Wh WALA,
1
Cent <1 so as dla
nt WAIN)
Hl
ii %Jo~ 0)
{
inPrime = 1;
break;
)
}
it iPr
cout“
Preudocode
Begin
Head a number 1
fri:200n
4y
Initial
Prime wo zero
forj:2t0 if
dy
i868 mod j iv equal 10 7210)
th
ize Prime to J
break
endif
Increment j value by J
loop
‘MGsPrime is equal to vero and n is n04
then
Init
ual 101)
print j
Increment i value by }
iv
“A iy
Big fo
i“ LTD.
UBLISHERS AND DISTRIBUTORS PVT
LINECOMPUTER SCIENCE PAPER-II
Flowchart
[Initialize the value of to 2|
Initialize j value to 2
=o
isPrime = 1 & break
[increment the value ofj by 1
increment the value
of:by 1
Step)
Q2. Write a program to find largest and
smallest elements in a given list of
numbers and sort the given list.
Answer :
Program
Hinclude
using namespace std;
int main Q
{
PROGRAMMING IN g
Tat [LOO]. 0. MAN.
cout << “Enter the
cin >>
<< “Enter the list of cumbers
for (i= 0:1 < i++)
cin >> IG:
if (max If)
min = If:
}
cout <<“Lare
cout << “Smallest element
for(i-Oxi
using namespace std;
class student
{
private:
char sname[20};
int sroll, smarks;
public:
void read();
void display();
LP.6
SIA PUBLISHERS AND DISTRIBUTORS PVT. LT:Yr
oyactical with Salutions
coral) ———__
——__ COMPUTER SCIENCE PAPER
Flowehart
[Create a student class
1
[Declare data members as|
Sname, Sroll, Smarks
1
|
| Declare input( ) and display()
ame:
“Enter student roll number?
nter student marks
display)
outs< “\nStudent Details:"<<« a
cou ta ‘
Begin using namespace std;
class Student class Employee
Declare data members as Sname, Sroll, Smarks | {
Function read( ) private:
Read the Sname, Sroll, Smarks iat
ad function input() ae : ‘ptr;
unction display
ae i rane. roll and Smarks ee
“nter total number of employees:”;
end fiction display( ) cout<<
End class student cin >> nm;
main function .
tr = new float{n];
create an object's” forthe student cass pir= new ee
i vrentotiest'S cout <<"aMemory allocated oF <<"
calli sion using st cen cc” << “employees"<< endl;
inpui()Functionusing stem ot a
call display() function using s™ cout <<“"\nEnter salary of employee 2
Endmsin << endl;
End oe pyt LID. —a—‘a“TPROGRAMING ty g
COMBOTES SOENCE PANES =
SD RSTASAAE MEANS cas |
Sk eS
ofn employees
Increment the value of i by 1
end loop
re an object ‘e” for the Employee cs
call displey{ ) fimction using Employ _
objet?
SIA PUBLISHERS AND DISTRIBUTORS PVT. L10-
Lsactical with Solutions
eee eee
[Create a Employee class
Lab Pr
Flowehart
———___
¥
[Declare data members as
1, “pte
{
[Create a constructor Employeet |
{to read the total number of
employees & allocate memory
¥
Tnitialize the value of ito 0
No
Read the salary ofall the
employees one by one
iat
J
Create a destructor ~Employee( )
to deallocate the memory
COMPUTER SCIENCE PAPER
“aren of reclangle is:
eepheend;
cout
}
Area(double r)
{
double e~3,14%r#5;
cout<< “area of circle is "5
cout
using namespace st
class Area
{
Public:
Area(int Lint b)
sie peaeetasiiead
I CAProgram FledDev-Cpp\rectaren cot
Pitiesicas tone
Beas ear eret
area of “square-is ;49.
Pseudocode
Begin
class Area
Constructor Area(int I, int b)
Print the area of rectangle as 1*b
‘end constructor Area( )
Constructor Area(double r)
Initialize variable ‘e’ 03.14 * rr
Print the area of circle as ¢
end constructor Area( )
Constructor Area(Integer s) with int *s* as
parameter
Print the area of square as s * s
end constructor Area( )
end class Area
main function
LP.9COMPUTER SCI
Create an object ‘obj! for the constructor
‘Area(int I int b) and pass parameters
Create an object ‘0bj2"for the constructor
Area (double r) and pass parameter
Create an object ‘obj3" for the constructor
‘Area (int s) and pass parameter
=]
End
Flowehart
Create a Area class
Create a constructor Area(integer |,
Integer b) to display to area of rectangle
—_—
(Create a constructor Area(double r) to
display the area of square
1
Create a constructor Areal integer 5) to
display the area of square
|
Create objects ‘objl’, “obj2", ‘obj3” for
Area class and pass the relevant parameters
Example(int al, int 61)
anal:
bobl;
Example(const Example &<)
a=ca;
bach;
4
void display)
t
concede” "< constructor Example{const Exexole &)
using namespace std; Assigncatoaandebtob
class Example end constructor Example)
{ function display( )
mines Print the values of 2 end b
int, be end function display{ )
inta, b; end class Example
pub) main function n
LP.10 SIA PUBLISHERS AND DISTRIBUTORS PVT."poctnat in Si
P Create object ‘el for Pox
es parauneters
Coeate whet 2” For Example elas ant
susigt 1 to 02
Print "Normal constructor
1 dieplayt ) function using
siject “ol ‘ising Example clay
print “copy constructor'*
call display() function usi
shject ly()Fmetion sing example clay
Creat a class Example
——_ fo
Declare data members as a, b|
end main
Und
jowchart
[Create constructor Example)
land assign al to a and bl tob
—_ fo
(Create constructor Example( )
land assign c.a to a and c.b to b
I
Declare display( ) function
to print a and b values
tea J
Create objects ‘el’ and ‘e2"
for Example class
Assign €1 to €2
Print Normal constructor
Call display( ) using
object ‘el’
Print Copy constructor
Call display() using
object *e2'
Gn
IRL leueme zum mieTRpIBUTORS PVT. LTD.
ple clay an
(PUTER SCIENCE PAPER-I
Q7. Write 9 program wining frlend factions:
and friond class,
Annwer 1 .
Program
Hinclude
std;
private:
int xy:
public:
Example()
{
cout<<"\nDefault Constructor"<
using namespace std:
class Example
{
int x,
Pseudocode
Begin
class Example
declare constructor Example( )
function show( )
print the values of x and y
end function show( )
end class Example
constructor Example( )
Initialize x to 10 and y to 20
print *Constructoris defined outsidetieo®
end constructor Example( )
main function
create an object ‘obj’ for Example cl
call show( ) function with object ‘9
of Example class
end main
End
publi
LP.1e
SIA PUBLISHERS AND DisTRIBUTORS PVT.”Create a class as Example
¥
Declare data members as x.y and
declare constructor Example( )
SS
Define show( ) functions to print
values of x and y
respectively
Print “constructor is defined
outside the class”
Create an object ‘obj? for
Example class
Print the values of x and y by
calling show() function using
object ‘obj
Implement all Three Constructors within a
Single Class as well as use Multiple Classes
(Individual Classes)
Program
private:
int xy;
public:
Example()
‘ :
cout<<“\nDefault Constructor in
class Example”<)
obj
Fam Al ype of constrains
indifferent eases
UTORS PVT. LTD, —————— L.19we
COMPUTER SCIENCE PAPER-It f
My
implement tho following CONCOPES USING Class ang gy” Mey,
q
Q9. Write a program to
(i) Function overloading
(i) Operator overloading (unary/binary(* and ~)).
Answer : “
@ Function Overloading
Program
#include
using namespace std;
int multiply(int, int); iifirst function
double multiply(double, int); __//second function ;
double multiply(double, double); //third function
int main( )
{
cout
using namespace std;
class Unary
void getdatal( )
{
cout<< “enter the value of x and ys
cin>>x>>Y3
3
void operator ++
{
Hx
cout<< “After incrementing x="
‘Obje
call display2() function using 4
@
Create a class as Unary
end mai
ind
art
Declare data members as x, y
Define function getdatal( ) to
read the values of x and y
{
Define function operator ++ ()|
to preincrement x
eee
Print “After incrementing x”
J
Create class as Binary
Declare data variables as
Define function getdata2() to
read the values of i, j
Define function operator + () and
create object ‘t’ for Binary class
‘Assign i + 02.ito t-i and
jt 02j tot
|) Define function display1( ) to print
| “Addition Result (i + i and j +j):”
Define function display2( ) to print
“Substraction Result (i- i and j—j)”
COMPUTER SCIENCE PAPER-I!
‘ct of class ‘Binary
Sult2" object of class “Binary”
[4
Create an object “obj”
for class Unary
1
Create an object ‘objl”
for class Unary
a
Create objects “obj?” “obj3",
resultl, result2 for class Binary
1
Read the values of x and y by calling
getdatal() function using object ‘obj1y
V
Preincrement obj and
predecrement obj
V
Print “Binary Operators”
Read the values of rand} by calling
‘getdata2( ) function using objects
*‘obj2’ and ‘obj3” of class Binary
Assign obj2 + obj3 to result!
and obj2 — obj3 to result?
Print the Addition result by calling.
display1() function using “result!”
object of class Binary
Print the subtraction result by calling
display2() function using result?”
object of class Binaryeet eee re
multiplo Inhoritances,
Anawer 1
Hroyeam
Hinelue “jostrenm
wsinye mamnespace tl,
elas tite
'
chine at] 505
public:
void petotate( )
1
cout Enter state mane;
chink;
}
voi displayat()
{
cous “State names" <<" <
using namespace std;
class parent
public:
1 parent(void)
{
void initializeg;
i
template
parent(C & child)
{
child initialize;
B
class child : public parent
{
public:
child(void): parent(*this)
{
cout<< “The parent constructor is overloaded in the child class\n”;
3
void initialize(void)
{
cout << “\nchild::initialize()” << endl;
B
int main(void)
{
child ¢1;
}
Output
TH) CAUsers\C3\Desktop\constructor_overloading exe
‘GpSCEeropiscerriye) i
The parent constructor is overloaded in the chila Fe
SIA PUBLISHERS AND DISTRIBUTORS PVT.L?Sail. anitietine ()
ext seus pace
exaie + vies Child derived Soon pore
wordy: parent" this)
yer
weet in thee
enh cuscucer cht
Soraction mattiadize ()
prise “ideo
eat Sues
ai Sacto
reais mo Giles Sor tae obi claws
enh sain
ese utero)
ie per mascara
—
(ili eB. TE
Sign ome siese
v
(Darian a PTE IDEIEE |
weg “hie esc epee
a
v
Pee Tie IOI
recta te
.
Tas wala ue
tae peste? J
a
Tae Taine Faas OHA)
roesian we barter
t
Gay
O12. Write 2 program to implement the
polymorphism and the following concepts
using cless and object
(i) Virtual functions
(ii) Pure virtual functions.
Answer:
G) Virtes! Fusctivns
Progress
Finclude ores
using namerpace sid;
class Pecangle
:
public:
fom lea, br,
Peccigie )
Pecnecgielficat |, fost b)
3
viral void compzte( )
{
Host x=20;
Bost y= len br,
Rays
cou<< “Perimeter of the
Rectangle <<:
5
B
less Azea: public Rectangle
t A
poblic:
void compute)
f
coutc< “Area of the
Rectangle:"<
sing namespace td
Jass Example
t
public:
viral void demo
i
ass A public Example
‘
publicweid demot)
i
outee Tisis clase As
)
»
lass B: public Example
‘
publics deat)
(
coute “This sas 8
,
lass C: publi Example
‘
publcwoid demot)
(
cout Mais class Cs
PROGRAMMING it ¢.,,
Pseudocode
Begin
clas Example
Detar a vita
nd elas Example
class A dorved from class Example
function demo )
print “This ie class A”
cad lass A
lass B derived from class Example
function demo)
print“This is class 8”
end fonction demo)
end cle 8
class C derived from class Example
«all demo) faetion one by one
end main
ad
~~ SIA PUBLISHERS AND DISTRIBUTORS PVT. LTD.
COMPUTER SCIENCE PAPER
(973. Wit program to impteme
‘Greate a class as Erample
puerae 5. rae
Declare demot) ure
ass Shape
i
s
prot
Shape (S= 05:
publi:
‘virtual Shope) 1
1
aray{2 respectively
‘
Rs PVT.LTD. —————— 18
SIA PUBLISHERS AND DISTRIBUTO!ComPUTEN SCIENCE PAPER
q
coat << angledai
,
TH)
fi
: cout << "Tring
Shope: Shepetope 0)
1
itch)
fl
ase CIR:S = new Cis breaks
case SQR: §=new Sar: break:
ase TRI:S = new Tis break;
,
aw; 1 Vrs)
1.36 —-—______ SIA PUBLISHERS AND DISTRIBUTORS PVT. LTD.
>
the constrictor
conto shape)
:
Ps ves
mi)
if
sp
ond
jsirvtor shape)
ea)
ends shape
end destructor it)
end class cit
class sop
éonstrctor ae)
end constructor sa
inetion draw)
P
nd funtion draw()
squared"
destructor ~sqr()
“square”
SIA PUBLISHERS AND DISTRIBUTORS PVT. LTD.
pointer forshape class
enumerator 2s Po
sede parameserto sh) constr
ints to dw) function
cic derived ftom clas shape
SOMPUTER SCIENCE PAPE! i
TH drive mas shape
)
angles"
int Tenge
end esctor=Ti)
end cass
onaraso sap
sit
ase IR
tet new CIR objet
break
180 SQR
reste new SQR object
rea
cae TRI
crete new TRL oie
tweak
cedswich
exon) funtion
‘Assign CIR,SQR, TRI shape objets
topointer
Declare constants ofnteges type and i
sign sizeof eiaz0f tt
tl funtion eal
a
for i002
sf pins oda) faretion all
Increment the valu of by 1
ed for lop
fordtose
print ===
aLP.s8
SIA PUBLISHERS AND DISTRIBUTORS PVT. LTD.
SIA PUBLISHERS AND DISTRIBUTORS PI
COMPUTER SCIENCE PAPER“
v.10, ——————— °°
as a aeCOMPUTER SCIENCE PAPE!
ineluderiosteam>
‘sing namespace sds
class Add
(
public:
void sumtin int b)
‘
coutceasbecendl:
‘courcearbreccendl
)
int main)
'
Add obit
obj. sum(10,20),
bj sum(10, 20, 30);
)
Output
Paeudocode
Begin
clas Add
uneton sur(in a, int)
print sum ofa and b as a+
nd funtion sum)
function sum(it a,
print sum of, 6, eas a¥b¥e
‘ed function sum)
end class Add
main function
create object ‘ob for class Add
«all faction sur) with wo parameters
using object ob
function sum) with hres parameters
Bj
end main
End
Flowchart
Dafne sn) Taion
rameters Be
Graco
for class Add
parameters using object ‘ob
G45. Write a program to demonstrate dynani
polymorphism using method overtidg
{and dynamie method dispatch,
Answer +
Program
include
using namespace std;
clase Student
‘
void branch()
‘
coute<"This is parent c
< obi3s
cout Enter an integer valu";
ol.getlat:
cout <*Bnler a double values
obj. getdate
obj. aisplayOs
return;
,
—— tts
ein means ceuens AND DISTRIBUTORS PVT.LTD. uCOMPUTER SCIENCE PAPER:
Pseudocode
Begin
(create a template “TI”
clas square
plate “TI” Fanetion
create an object,
pevdata)
Read x
end funtion getdatat)
function display)
print he result
end function display}
End class square
‘main function
create objec
create object‘
create object ‘bj3"for ess square
for class square
for class square
function using object “obj
‘quare
call getdet() function and display()
square
all getdata()funetion and display)
function using object ‘obj3" of class
square
End main
End
IA PUBLISHERS AND DISTRIBUTORS
(Deine grr) Fioneton]
end vale
‘ea.
[ene ,
resperively
gett) ai
class square n>
value by calling
sing object ‘obj of
“double
pvt. Lo