IDNLearn.com: Your trusted platform for finding reliable answers. Our experts are available to provide accurate, comprehensive answers to help you make informed decisions about any topic or issue you encounter.

Given an integer representing a 10-digit phone number, output the area code, prefix, and line number, separated by hyphens.
Ex: 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 are 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 that integer division discards the fraction).
For simplicity, assume any part starts with a non-zero digit. So 999-011-9999 is not allowed.
LAB: Phone number breakdown 0/100
1 phone_number = int(input)
2
3 num_one = s[:3]
4 num_two = s[3:6]
5 num_three = s[6:]
6
7 final_number = '('+num_one+')'+num_two+'-' +num_three
8 print = final_number


Sagot :

Answer:

In Python:

phonenum = int(input("10 digit phone number: "))

last4 = phonenum%10000

phonenum//=10000

mid3 = phonenum%1000

phonenum = phonenum//1000

newnum = str(phonenum)+"-"+str(mid3)+"-"+str(last4)

print("New: "+str(newnum))

Explanation:

This prompts the user for phone number

phonenum = int(input("10 digit phone number: "))

This gets the last 4 of the phone number using % operator

last4 = phonenum%10000

This shifts the phone number by 4 digit right

phonenum//=10000

This gets the middle 3 of the phone number using % operator

mid3 = phonenum%1000

This shifts the phone number by 3 digit right

phonenum = phonenum//1000

This generates the new phone number which includes the area code...

newnum = str(phonenum)+"-"+str(mid3)+"-"+str(last4)

This prints the formatted phone number

print("New: "+str(newnum))

We value your presence here. Keep sharing knowledge and helping others find the answers they need. This community is the perfect place to learn together. IDNLearn.com is your go-to source for accurate answers. Thanks for stopping by, and come back for more helpful information.