0% found this document useful (0 votes)
47 views2 pages

Nafees 1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

Solution 1:

#include <iostream>
using namespace std;

struct phone
{
int area; //area code (3 digits)
int exchange; //exchange (3 digits)
int number; //number (4 digits)
};

int main()
{
phone ph1 = { 212, 767, 8900 }; //initialize phone number
phone ph2; //define phone number

// get phone no from user


cout << "\nEnter your area code, exchange, and number";
cout << "\n(Don’t use leading zeros): ";
cin >> ph2.area >> ph2.exchange >> ph2.number;

cout << "\nMy number is " << '(' << ph1.area << ") " <<
ph1.exchange << '-' << ph1.number;
cout << "\nYour number is " << '(' << ph2.area << ") "<<
ph2.exchange << '-' << ph2.number << endl;

return 0;

Solution 2:

#include <iostream>
using namespace std;

struct point
{
int xCo; //X coordinate
int yCo; //Y coordinate
};

int main()
{
point p1, p2, p3; //define 3 points
cout << “\nEnter 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;
}
Solution 3:

#include <iostream>
using namespace std;

class Rectangle {
public:
Rectangle( double = 1.0, double = 1.0 );
double perimeter( void );
double area( void );
void setWidth( double w );
void setLength( double l );
double getWidth( void );
double getLength( void );

private:
double length;
double width;
};

Rectangle::Rectangle( double w, double l )


{ setWidth(w); setLength(l);}

double Rectangle::perimeter( void )


{
return 2 * ( width + length );
}

double Rectangle::area( void )


{ return width * length; }

void Rectangle::setWidth( double w )


{ width = w > 0 && w < 20.0 ? w : 1.0; }

void Rectangle::setLength( double l )


{ length = l > 0 && l < 20.0 ? l : 1.0;}

double Rectangle::getWidth( void ) { return width; }


double Rectangle::getLength( void ) { return length;}

int main()
{
Rectangle a, b( 4.0, 5.0 ), c( 67.0, 888.0 );

// output Rectangle a
cout << "a: length = " << a.getLength()<< "; width = " <<
a.getWidth()<< "; perimeter = " << a.perimeter() << "; area = " <<
a.area() << '\n';

// output Rectangle b
cout << "b: length = " << b.getLength() << "; width = " <<
b.getWidth() << "; perimeter = " << b.perimeter() << "; area = " <<
b.area() << '\n';

// output Rectangle c; bad values attempted


cout << "c: length = " << c.getLength() << "; width = " <<
c.getWidth() << "; perimeter = " << c.perimeter() << "; area = " <<
c.area() << '\n';
return 0;
}

You might also like