Upload
sudha-palani
View
232
Download
0
Embed Size (px)
Citation preview
8/20/2019 OOPs Lab Madnual (1)
1/18
PRACTICAL NO:- 01
AIM:-Write a program to find out the factorial of a given number.
OBJECTIVE:- Factorial is represented using ‘!’. So seven factorial will be written as (7!).
Factorial of a given number ‘n’ can be find out by the formula
n(n"#)(n"$)%%%%%%%%%..$#
or n! & n(n"#)(n"$)%%%%%%%%%..$#
'ero factorial (!) is defined as one i.e. ! & #.
SOURCE CODE:-//rogram to find out the factorial of a given number%..
*include+iostream, --eader file
using namespace std/int main( )
0
int i1 n1 fact/
cout++2enter the number2/
cin,, n/
for (i/ i+&n/ i33)
0
fact & facti/
4cout++2the factorial is2++fact/
return /
cin.get()/
cin.ignore()/
4
OUTPUT:-
8/20/2019 OOPs Lab Madnual (1)
2/18
PRACTICAL NO:-02
AIM:- Write a program to check whether a given number is an Armstrong number ornot.
OBJECTIVE:- 5 number is 5rmstrong if the sum of cubes of individual digits of a numberis e6ual
to the number itself.
For eample 87# is an 5rmstrong number as
(888) 3 (777) 3 (###) & 87#.
SOURCE CODE:-
8/20/2019 OOPs Lab Madnual (1)
3/18
int main( )
0
int n 1arm&1digit1num/
cout++2enter a number2/
cin,,n/
num&n/
while (n,)
0
digit&n:#/
n&n-#/
arm&(digitdigitdigit)3arm/
4
if(num&&arm)
cout++num++2is armstrong2/
else
cout++num++2is not armstrong2/
return /
cin.get()/
cin.ignore()/
4
OUTPUT:-
8/20/2019 OOPs Lab Madnual (1)
4/18
Try it…..
C++ Pror!" to #$% &'r(')t $*"'r.
C++ Pror!" to #$% &ri"' $*"'r .
C++ Pror!" to !r'! !$% &'ri"'t'r o( tri!$,'.
PRACTICAL NO:- 0 ;ate
AIM:- Create the equivalent of a four function calculator. The program should request theuser to
enter a number an operator and another number. !t should then carr" out the specified
arithmetical
operation: adding subtracting multipl"ing or dividing the two numbers. #!t should use a
switch
statement to select the operation$. %inall" it should displa" the result.
When it finishes the calculation the program should ask if the user wants to do another
calculation. The response can be &'( or &)(.
OBJECTIVE" 5 Four Function calculator can be made by using
8/20/2019 OOPs Lab Madnual (1)
5/18
switch (opr)
0
case C3C result&num#3num$/
brea9/
case C"C result&num#"num$/
brea9/
case CC result&num#num$/
brea9/
case C-C result&num#-num$/
brea9/
default cout++2Dn?rong operator2/
4
Eoll no
#
cout++2Dn5nswerDt2++result/
cout++2Dnress for anotherDt2/
cin,,ch/
4 while((ch&&CC)GG(ch&&CyC))/
return /
cin.get()/
cin.ignore()/
4
8/20/2019 OOPs Lab Madnual (1)
6/18
Eoll no
##
OUTPUT:-
PRACTICAL NO:-0
AIM:- Write a function called reversit # $ that reverses a string #an arra" of char$. *se afor loop that swaps the first and last characters then the second and ne+t to last characters
and so on. The string should be passed to reversit # $ as an argument.
Write a program to e+ercise reversit # $. The program should get a string from the user callreversit# $ and print out the result.
8/20/2019 OOPs Lab Madnual (1)
7/18
ere we will use a for loop that swaps the first and last characters1 then the second and net to
last characters and so on. A a function which will reverse the input string.
SOURCE CODE:---rogram to reverse the input string..
*include+iostream, --eader file
using namespace std/
int reversit(char I J)/ --Function ;eclaration
int main( )
0
char string#IKJ/
cout++2Bnter the stringDn2/
gets(string#)/
reversit(string#)/ --Halling of Function reversit with input string
as argument.
return /
cin.get( )/ cin.ignore( )/
4
int reversit(char string#IKJ) --Function ;efinition
0
char string$IKJ/
int i1L19/
for (i&/string#IiJ!&CDC/i33)/
for (9&i1 L&/9,/9""1L33)
0
string$ILJ&string#I9"#J/
4
string$ILJ&CDC/
cout++2Dn2/
puts(string$)/
cout++2Dn>he reverse of input string is2/
puts(string$)/
return /
cin.get()/
cin.ignore()/
4
8/20/2019 OOPs Lab Madnual (1)
8/18
OUTPUT:-
8/20/2019 OOPs Lab Madnual (1)
9/18
PRACTICAL NO:- 0/
AIM:- ,aising a number n to a power p is the same as multipl"ing n b" itself p times.Write a function called power # $ that takes a double value for n and an int value for p and
returns the result as double value. *se a default argument of for p so that if this
argument is omitted the number will be squared.
Write a main# $ function that gets values from the user to test this function.
OBJECTIVE:- >he nth of power $ can be find out as
($$$%%%%%..n times)
A
@f the argument p&$ is omitted then S6uare of a given number n can be find out as
S6uare & number number.
SOURCE CODE:---rogram to calculate the power of a given number%..
*include+iostream, --eader file
using namespace std/
int power (int1 double)/ --Function ;eclaration of power
int s6uare (int)/ -- Function ;eclaration of s6uare
int main( )
0
int a/
double result/
cout++2enter a number2/
cin,,a/
result& power(a1 $)/ --Function calling
cout++2power of the number isDt2++result++=Dn=/
s6uare(a)/ --Function calling
return /
4
int power (int n1 double p ) --Function ;efinition of power
0 int c i/
for (i/ i+&n/ i&i3#)0
c cp/
8/20/2019 OOPs Lab Madnual (1)
10/18
int s6uare (int a) -- Function ;efinition of s6uare
0
int c/
c&aa/
cout++2s6uare of the numberDt2++c/
return /
4
OUTPUT:-
AIM:- Write a C rograms to *nderstand 0ifferent %unction Call mechanism
8/20/2019 OOPs Lab Madnual (1)
11/18
OBJECTIVE:-
Swapping of two number by both call by value and call by reference mechanism1 using two
functions swapMvalue() and swapMreference respectively 1 by getting the choice from the user and
eecuting the user’s choice by switch"case.
SOURCE CODE:-*include+iostream.h,
*include+conio.h,
void main(void)
0 clrscr()/ int 1y1n/
void swap#(int1int)/
void swap$(int1inty)/
cout++2enter two values to be swappedDn2/
cin,,,,y/
cout++2Dnfor H5NN O P5NQB press#2/ cout++2Dnfor H5NN O EBFBEBRHB press$2/
cin,,n/
switch(n)
0
case #
cout++2DnH5NN O P5NQB2/ cout++2Dnvalues before swap()2++++2Dt2++y/ swap#(1y)/
cout++2Dnafter swap outside of swap#()2/
cout++2Dn&2++++2Dny&2++y/
brea9/
case $
cout++2DnH5NN O EBFBEBRHB2/ cout++2Dnvalue before swap2++++y/ swap$(A1Ay)/
cout++2Dnafter swap(outside of swap$)2++++y/
brea9/
4
getch()/
4
void swap#(int 1int y)
0
int temp/ temp&/ &y/ y&temp/cout++2Dnswapped values(inside swap#())2++++2Dt2++y/
4
void swap$(int 1int y)
0
int temp/
temp&/
&y/
y&temp/
cout++2Dnswapped value(inside swap$())2++++2Dt2++y/
4
8/20/2019 OOPs Lab Madnual (1)
12/18
Try it……
PRACTICAL NO:- 0/
AIM: C++ program to demonstrate the concept of classes with primitive data
8/20/2019 OOPs Lab Madnual (1)
13/18
Create a 10!2TA)C31 class with :
- feet and inches as data members
- member function to input distance
- member function to output distance
- member function to add two distance ob4ects
Write a main function to create ob4ects of 0!2TA)C3 class. !nput two distances and
output the sum.
SOURCE CODE:-
*include+iostream.h,
*include+conio.h,
class
distance0 int feet1inch/
public
void getdistance()/void putdistance()/
void adddistance(distance d#1distance d$)/
4/
void distancegetdistance()
0cout++2DnBnter the feet 2/
cin,,feet/cout++2DnBnter the inches 2/
cin,,inch/
4
void distanceputdistance()
0
cout++2DnDnfeet & 2++feet/cout++2Dtinch & 2++ inch/
4void distanceadddistance(distance d#1distance d$)
0
4
int
main()
0
inch&d#.i
nch3d$.in
ch/feet&inch-
#$/
inch&inch
:#$/
feet&feet3
d#.feet3d
$.feet/
8/20/2019 OOPs Lab Madnual (1)
14/18
distance d#1d$1d8/
clrscr()/cout++ 2Dn;istance # Dn2/
d#.getdistance()/
cout++ 2Dn;istance $ Dn2/d$.getdistance()/d8.adddistance(d#1d$)/
cout++2Dn>he Sum f two distance is 2/
d8.putdistance()/
getch()/return()/
4
OUTPUT:
;istance #
Bnter the feet #8
Bnter the inches ##
;istance $
Bnter the feet ##
Bnter the inches ##
>he Sum f two distance is
feet & $T inch & #
8/20/2019 OOPs Lab Madnual (1)
15/18
PRACTICAL NO:-1D!t'
AIM:- A hospital wants to create a database regarding its indoor patients. Theinformation to store include
a$ )ame of the patient
b$ 0ate of admission
c$ 0isease
d$ 0ate of discharge
Create a structure to store the date #"ear month and date as its members$. Create a base
class to store the above information. The member function should include functions to
enter information and
displa" a list of all the patients in the database. Create a derived class to store the age of the
patients. 5ist the information about all the to store the age of the patients. 5ist the
information about all the pediatric patients #less than twelve "ears in age$.
OBJECTIVE :- ere we use the concept of both structure and the class. ?e declare structureglobally which store the value of date i.e. year1 month1 and date and a class which store the above
given information of patients. ere an obLect of structure is declared in the class and is
accessible using dot (.) operator.
SOURCE CODE:---rogram to print the patients details%%%%..
*include+iostream, --eader file
using namespace std/
struct date --;eclaration of a Structure
0
int d1y/
char mI$J/
4/
class patients
0
char nameI8J/
date dMofMadd/
char diseaseI8J/date dMofMdis/
public
void getdata( )
0
cout++2DnBnter the name of the patient2/
cin,,name/
8/20/2019 OOPs Lab Madnual (1)
16/18
cout++2Bnter the date of admission2/
cin,,dMofMadd.d,,dMofMadd.m,,dMofMadd.y/
cout++2Bnter the name of disease2/
cin,,disease/
cout++2Bnter the date of ;ischarge2/
cin,,dMofMdis.d,,dMofMdis.m,,dMofMdis.y/
4
void display( )
0
cout++2Dn>he name of patient is2++name/
cout++2Dn>he date of admission is2++dMofMadd.d++2"2++dMofMadd.m++2"2++dMofMadd.y/
cout++2Dn>he name of disease is2++disease/
cout++2Dn>he date of ;ischarge is2++dMofMdis.d++2"2++dMofMdis.m++2"2++dMofMdis.y/
4
4/
class patientsMage public patients
0
public
int age/
publicvoid getdata( )
0
patientsgetdata( )/
cout++2Bnter the age of the patient2/
cin,,age/
4
void display( )
0
patientsdisplay( )/
cout++2Dn>he age of the patient is2++age/
4
4/
int main( )
8/20/2019 OOPs Lab Madnual (1)
17/18
0 patientsMage pI8J/
for (int i&/i+8/i33)
0
cout++2Dnatient no2++i3#/
pIiJ.getdata()/
4
cout++2Dn>he list of all the patients2/
for (int i&/ i+8/ i33)
0
cout++2Dnatient no2++i3#/
pIiJ.display( )/
cout++2Dn2/
4
cout++2Dn>he list of all the pediatric patients (less than twelve years in age)2/
for (int i&/ i+8/ i33)
0
if(pIiJ.age+#$)
pIiJ.display()/
cout++2Dn2/
4
return /
cin.get( )/
cin.ignore( )/
4
8/20/2019 OOPs Lab Madnual (1)
18/18
OUTPUT:-