Friday 11 August 2017

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



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
SOLUTION:
#include<iostream.h>
#include<conio.h>
////////////////////////////////////////////////////////////// Define Structure /////////////////////////////////////////////////////////////
struct phone
            {
int area_code,exchange,number;
};
///////////////////////////////////////////////////// Define Structure Variables /////////////////////////////////////////////////////
phone num1={212,767,8900},num2;
void main()
{
clrscr();
///////////////////////////////////////////////////////////////// Take Input ////////////////////////////////////////////////////////////////////
cout<<"Enter your area code ";
                        cin>>num2.area_code;
cout<<"Enter your exchange ";
                        cin>>num2.exchange;
cout<<"Enter your number ";
                        cin>>num2.number;
////////////////////////////////////////////////////////////////////// Output /////////////////////////////////////////////////////////////////////
cout<<"\nMy number is ("<<num1.area_code<<") ";
cout<<num1.exchange<<"-"<<num1.number;
cout<<"\n\nYour number is ("<<num2.area_code<<") ";
cout<<num2.exchange<<"-"<<num2.number;
getch();
}
OUTPUT:

0 comments: