Given a long long integer representing a 10-digit phone number, output the area
ID: 3876883 • Letter: G
Question
Given a long long integer representing a 10-digit phone number, output the area code, prefix, and line number, separated by hyphens. If the input is 8005551212, the output is: 800-555-1212 Hint: Use % to get the desired rightmost digits. Ex: The rightmost 2 digits of 572 is gotten by 572 % 100, which is 72. Hint: Use/ to shift right by the desired amount. Ex Shifting 572 right by 2 digits is done by 572/100, which yields 5. (Recall integer division discards the fraction). For simplicity, assume any part starts with a non zero digit. So 999-011.999 is not allowed. LAB ACTIVITY 2.22.1: CH2 LAB: Phone number breakdown 0 10 main.cpp Load default template.. 1 Rinclude clostrean 2 using namespace std 4 int main) ( S long long phoneNumber; 7cin > phonelunber 9Type your code here / 10 11 return ; 12 13Explanation / Answer
#include <iostream>
using namespace std;
int main() {
long long phoneNumber;
cin >> phoneNumber;
// taking last 4 digits for lineNumber
int lineNumber = phoneNumber % 10000;
// removing last 4 digits from phoneNumber
phoneNumber = phoneNumber / 10000;
// taking last 3 digits now from phoneNumber
int prefix = phoneNumber % 1000;
// removing last 3 digits
phoneNumber = phoneNumber / 1000;
// giving code, the phoneNumber
int code = phoneNumber;
// printing output
cout << code << "-" << prefix << "-" << lineNumber;
}
/* SAMPLE OUTPUT
8005551212
800-555-1212
9876543219
987-654-3219
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.