PRACTICAL FILE 2322813
of
“OBJECT ORIENTED PROGRAMMING LAB”
SUBJECT CODE : PC-CS-AIML-205A
SUBMITTED IN PARTIAL FULFILLMENT OF THE REQUIREMENTS FOR
THE AWARD OF
BACHELORS OF TECHNOLOGY (B.TECH.)
IN
Artificial Intelligence and Machine Learning
(SESSION: 2022-26)
SUBMITTED BY:
Mahima Gupta
2322813
BRANCH: Artificial Intelligence and Machine Learning
UNDER THE SUPERVISION OF:
Er. Seema Rani
Assistant Professor
CSE Department, ACE
Department of Computer Science and Engineering
Ambala College of Engineering and Applied Research, Devsthali, Ambala
(Haryana)
Affiliated to Kurukshetra University,
Kurukshetra
2322813
INDEX
S.NO PRACTICAL AIM DATE SIGNATURE
1. To find the power of a number.
2. To add two Co-ordinates.
To make a calculator to perform
functions like addition,
3. 13-10-2023
subtraction, multiplication and
division.
To store the three parts of a
4. 13-10-2023
phone number separately.
To add the values in the format
5. of feet and inches or meters and
centimeters.
6. To add two Rational Numbers.
To find age of daughter and son
7.
based on the age of father.
To create a binary file by reading
8.
the data.
To create a database which can
9.
store the information of a patient.
10. To test the function.
2322813
PRACTICAL-1
AIM-1 :Raising a number to a power p is the same as multiplying n by
itself p times. Write a function called power that takes two arguments, a
double value for n and an int value for p, and return the result as double
value. Use default argument of 2 for p, so that if this argument is omitted
the number will be squared. Write the main function that gets value from
the user to test function.
Software Used: Code Block
Procedure:
1. Make a function to calculate power such that:
a. Define two parameters , one as number and other as power
with default valueas 2.
b. In function set new variable x with value 1.
c. Start for loop with variable I initialize with zero value up to less than p.
d. Now x=x*n; until for loop.
e. Return value of x.
2. Continue main function.
3. Enter the value of number and enter power.
4. Call power function with argument passing.
5. Now print result with power passing and without power passing(Default
power is 2).
SOURCE CODE:
#include<iostream>
using namespace std;
double power(double,int=2);
int main()
{
int p;
double n,r;
cout <<"Enter number : ";
cin >> n;
cout <<"Enter exponent : ";
cin >> p;
r = power(n,p);
cout <<"Result is "<< r;
r = power(n);
2322813
cout <<"\nResult without passing exponent is "<< r;
return 0;
}
double power(double a, int b)
{
double x = 1;
for(int i = 1; i <= b; i++)
x = x * a;
return x;
}
OUTPUT:
Enter number : 9
Enter exponent : 4
Result is 6561
Result without passing exponent is 81
RESULT:
2322813
PRACTICAL-2
AIM-2: A point on the two-dimensional plane can be represented by two numbers: an x
coordinate and a y
coordinate. For example, (4,5) represents a point 4 units to the right of the vertical axis, and 5
units up from the horizontal axis. The sum of two points can be defined as a new point whose
x coordinate is the sum of the x coordinates of the two points, and whose y coordinate is the
sum of the y coordinates. Write a program that uses a structure called point to model a point.
Define three points,
and have the user input values to two of them. Then set the third point equal to the sum of the
other two, and display the value of the new point. Interaction with the program might look
like this:
Enter coordinates for p1: 3 4
Enter coordinates for p2: 5 7
Coordinates of p1+p2 are: 8, 11
Software Used: Code Block
Procedure:
1. First create a Structure named as ‘point’, to store value of two coordinates of
a point.
2. In main function enter the coordinates for two points , p1 & p2 using
‘point’ structure.
3. For sum of coordinates of p1 & p2 , use ‘+’ operator and store value of
the coordinates ina new variable,p3.
4. The print p3 coordinates as required using ‘point’ structure.
SOURCE CODE:
#include <iostream>
using namespace std;
struct point
{
int x Co ; //X coordinate
int y Co ; //Y
coordinate
};
int main ()
2322813
{
point p1, p2, p3; //define 3 points
cout << "\n Enter coordinates for p1: "; //get 2 points
cin >> p1.xCo >> p1.yCo; //from user
cout << "Enter coordinates for p2: ";
cin >> p2.xCo >> p2.yCo;
p3.xCo = p1.xCo + p2.xCo; //find sum of
p3.yCo = p1.yCo + p2.yCo; //p1 and p2
cout << "Coordinates of p1 + p2 are : " //display the sum
<< p3.xCo << ", " << p3.yCo << endl;
return 0;
}
OUTPUT:
Enter coordinates for p1: 4,6
Enter coordinates for p2: 8,2
Coordinates of p1 + p2 are : 12,8
Date of performing
2322813
13-10-23
PRACTICAL-3
AIM-3 : Create the equivalent of a four-function calculator. The program should ask the user
to enter a number, an operator, and another number. (Use floating point.) It should then carry
out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two
numbers. Use a switch statement to select the operation. Finally, display the result. When it
finishes the calculation, the program should ask whether the user wants to do another
calculation. The response can be ‘y’ or ‘n’.
Software Used: Code Block
Procedure:
1. Include necessary header files
2. Declare variables to store user input and the result
3. Create a do-while loop to repeat the calculation until the user decides to exit
4. Finally, the program will continue to perform calculations until the user enters 'N'
when asked if they want to do another calculation.
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
double n1, n2, ans;
char oper, ch;
do {
cout << "\nEnter first number, operator, second number : ";
cin >> n1 >> oper >> n2;
switch (oper)
{
case '+': ans = n1 + n2; break;
case '-': ans = n1 - n2; break;
case '*': ans = n1 * n2; break;
case '/': ans = n1 / n2; break;
default: ans = 0;
}
cout << "Answer = " << ans;
cout << "\nDo another(Enter ‘Y’ or ‘N’) ? ";
2322813
cin >> ch;
} while (ch != 'N');
return 0;
OUTPUT:
Enter first number, operator, second number : 25-12
Answer = 13
Do another(Enter Y or N) ? N
2322813
Date of
performing 13-10-
23
PRACTICAL-4
AIM-4 : A phone number, such as (212) 767-8900, can be thought of as having three parts: the
area code
(212), the exchange (767) and the number (8900). Write a program that uses a structure to
store these three parts of a phone number separately. Call the structure phone. Create
two structure variables of type phone. Initialize one, and have the user input a number
for the other one. Then display both numbers. The interchange might look like this:
Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
• Your number is (415) 555-1212
Software Used: Code Block
Procedure: Step -1 : Write the C Code
Copy and paste the following code into your phone numbers.c file Step
2: Save the File.
Save the phone_numbers.c file.
Step 3: Compile the Program
Compile the program using your C compiler. If you’re using GCC, open your terminal and navigate
to the directory where phone_numbers.c is saved, then compile it with:
6. Make a function to calculate power such that:
a. Define two parameters , one as number and other as power
with default valueas 2.
b. In function set new variable x with value 1.
c. Start for loop with variable I initialize with zero value up to less than p.
d. Now x=x*n; until for loop.
e. Return value of x.
7. Continue main function.
8. Enter the value of number and enter power.
9. Call power function with argument passing.
10. Now print result with power passing and without power passing(Default
power is 2).
SOURCE CODE:
#include <iostream>
2322813
using namespace
std; struct Phone
{
int
area_code;
int
exchange;
int number;
};
int main()
{
Phone phone1 = {212, 767,
8900}; Phone phone2;
cout << "Enter a phone number (area code exchange
number): "; cin >> phone2.areaCode >>
phone2.exchange >> phone2.number;
cout << "Phone 1: (" << phone1.areaCode << ") " << phone1.exchange << "-"
<< phone1.number << std::endl;
cout << "Phone 2: (" << phone2.areaCode << ") " << phone2.exchange << "-"
<< phone2.number << std::endl;
return 0;
}
OUTPUT: