Answer:
#include <iostream>
using namespace std;
int main()
{
//declare variable to store phone numbers,its area code, prefix and line number.
long phone_number;
int area_code, prefix,line_number;
cout<<"Enter 10-digit phone number:"<<endl;
//input 10-digit phone number
cin>>phone_number;
//logic to find area_code, prefix, and line_number.
area_code = phone_number/10000000;
prefix = (phone_number/10000)%1000;
line_number = phone_number%10000;
//output phone number in asked format.
cout<<area_code<<"-"<<prefix<<"-"<<line_number<<endl;
return 0;
}
Output:
Enter 10-digit phone number:
8005551212
800-555-1212
Explanation:
In the above program 10 digit phone number entered by user will be stored in the variable phone_number.
Then using this phone_number variable area_code, prefix, and line number are calculated using the following logic:
area_code = phone_number/10000000;
prefix = (phone_number/10000)%1000;
line_number = phone_number%10000;
Finally these area_code, prefix, and line_number are displayed with hyphen in between using cout statement.