0% found this document useful (0 votes)
12 views

Oops File

The third document creates a basic four function

Uploaded by

Preeti Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Oops File

The third document creates a basic four function

Uploaded by

Preeti Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

OBJECT ORIENTED PROGRAMMING FILE

UNIVERSITY INSTITUTE OF ENGINEERING AND


TECHNOLOGY, KURUKSHETRA UNIVERSITY,
KURUKSHETRA

Submitted by: Submitted to:


Shivangi Sharma Mrs. Sonia
252202029 Assistant Professor
CSE-A Dept. of CSE
PROGRAM 1

Raising a number n to a power p is the same as multiplying n by 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 a double value. Use a default
argument of 2 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.
SOURCE CODE:
#include<iostream>
using namespace std;
double power(double n, int p=2);
int main() {
double pow , n;
cout<<"Enter the value of n:";
cin>>n;
cout<<"Case 1: ";
pow= power(n);
cout<<"Case 2: ";
pow= power(n,5);
cout<<"Case 3: ";
pow= power(3,4);
return 0;
}

double power(double n ,int p)


{
double ans = 1;
for(int i=0; i<p; i++)
{
ans = ans*n;
}
cout<<"n="<<n<<" and p = "<< p << endl;
cout<<" n(" << n << ") raised to the power p("<< p <<") is "<< ans <<endl;
return ans;
}
OUTPUT:
PROGRAM 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 origin along the X axis and 5 units up the Y 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 points and whose Y coordinate is the sum of
their 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

SOURCE CODE:
#include <iostream>
using namespace std;
struct point{
int x;
int y;
};
point sum(point p1, point p2);
int main()
{
point p1, p2, p;
cout<<"Enter the coordinates of point p1: ";
cin>>p1.x>>p1.y;
cout<<"Enter the coordinates of point p2: ";
cin>>p2.x>>p2.y;
p = sum(p1,p2);
cout<<"Coordinates of point p are: "<<p.x<<","<<p.y;
return 0;
}
point sum(point p1, point p2)
{
point p3;
p3.x = p1.x + p2.x;
p3.y = p1.y + p2.y;

return p3;
}
OUTPUT:
PROGRAM 3

Create the equivalent of a four function calculator. The program should


request the user to enter a number, an operator, and another number. It
should then carry out the specified arithmetical operation: adding,
subtracting, multiplying, or dividing the two numbers. (It should use a
switch statement to select the operation). Finally it should display the result.
When it finishes the calculation, the program should ask if the user wants to
do another calculation. The response can be ‘Y’ or ‘N’. Some sample
interaction with the program might look like this.
Enter first number, operator, and second number: 10/ 3
Answer = 3.333333
Do another (Y/ N)? Y
Enter first number, operator, second number 12 + 100
Answer = 112
Do another (Y/ N) ?

SOURCE CODE:
#include<iostream>
using namespace std;
int performoperation()
{
int num1,num2,result;
char operation,perform;
cout<<"Enter first number:";
cin>>num1;
cout<<"Enter second number:";
cin>>num2;
cout<<"Enter the operation to be performed:";
cin>>operation;
switch(operation){
case '+': result=num1+num2;
cout<<"Sum is:"<<result;
break;
case '-': result=num1-num2;
cout<<"Difference is:"<<result;
break;
case '*': result=num1*num2;
cout<<"Product is:"<<result;
break;
case '/': result=num1/num2;
cout<<"Division is:"<<result;
break;
default:
cout<<"Invalid Operation";
}
cout<<endl<<"Perform Again (Y/N)";
cin>>perform;
if(perform=='Y'){
performoperation();
}
else{
cout<<"Thank you";
}
return 0;
}
int main(){
performoperation();
return 0;
}
OUTPUT:

You might also like