Question

Write a C program that accepts a phone number of the form +1(xxx)-xxx-xxxx where x is...

Write a C program that accepts a phone number of the form +1(xxx)-xxx-xxxx

where x is a digit, and displays the sum of all digits in the phone number.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
int main(){
char phone[20] ;
int a;
int total = 0;
printf("Enter the phone number in the form +1(xxx)-xxx-xxxx \n");
//Getting the phone number and save it in the character array
gets(phone);
//for loop is to iterate over the character array to find the total
for(a = 2; a <= 20; a++){//initial a= 2 to ignore the '+1'
switch(phone[a]){
case '1':
total = total+1;
break;
case '2':
total = total+2;
break;
case '3':
total = total+3;
break;
case '4':
total = total+4;
break;
case '5':
total = total+5;
break;
case '6':
total = total+6;
break;
case '7':
total = total+7;
break;
case '8':
total = total+8;
break;
case '9':
total = total+9;
break;
default:
total= total+0;
break;
}
}
printf("Total of the digits of the phone number is %d", total);
return 0;
}

Enter the float value in line 1, Character in the next line, integer in next line and Character in the 4th line: 1234.56 a 50

Add a comment
Know the answer?
Add Answer to:
Write a C program that accepts a phone number of the form +1(xxx)-xxx-xxxx where x is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program using Visual Studio 2017 that accepts four (4) lines of input: • The...

    Write a program using Visual Studio 2017 that accepts four (4) lines of input: • The first line contains a float value in 4.2f format • The second line contains a char value • The third line contains a 4-digit int value • The fourth line contains a char value and then displays all of the input on a single line Phone Number Write a program that accepts a phone number of the form +1(xxx)-xxxxxxx where x is a digit,...

  • The convert_phone_number function checks for a U.S. phone number format: XXX-XXX-XXXX (3 digits followed by a...

    The convert_phone_number function checks for a U.S. phone number format: XXX-XXX-XXXX (3 digits followed by a dash, 3 more digits followed by a dash, and 4 digits), and converts it to a more formal format that looks like this: (XXX) XXX-XXXX. Fill in the regular expression to complete this function. 1 4 import re 2 - def convert_phone_number(phone): 3 result = re.sub) return result 5 6 print(convert_phone_number("My number is 212-345-9999.")) # My number is (212) 345-9999. 7 print(convert_phone_number("Please call 888-555-1234"))...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • Phone Number Problem (Use Object-Oriented Programming Style. Otherwise there will be no credit) Write a C++ program tha...

    Phone Number Problem (Use Object-Oriented Programming Style. Otherwise there will be no credit) Write a C++ program that can convert any 7-digit telephone number into corresponding words where the equivalence of each digit and its corresponding characters is defined in the telephone keypad, which is shown in the following table: ABC 4 1 JKL MNO 8 TUV WXYZ In the keypad, there is no equivalence for digit 1 and 0, but in reality, the phone number might contain those two...

  • Problem 1: Write a program that reads a positive float number and displays the previous and...

    Problem 1: Write a program that reads a positive float number and displays the previous and next integers. Sample Run: Enter a float: 3.5 The previous and next integers are 3 and 4. Problem 2: Write a program that reads two integers and displays their sum, difference, product, and the result of their division. Sample Run: Enter two integers: 8 5 Sum: 8, Difference: 3, Product: 40, Division: 1.6 Problem 3: Write a program that reads a three-digit integer from...

  • Q1. Write program calculate the final price of a purchased item using values entered by the...

    Q1. Write program calculate the final price of a purchased item using values entered by the user Hint: you are required to use formatting output for number, currency and percentage. --------------------[Print Receipt] ------------ Enter the quantity: 6 Enter the unit price: $1.98 Subtotals: $10.14 Tax: $ 0.61 at 6% Total: $10.75 ------------------------------------------------ Q2. Write a program that prompts for and reads the users’ first name and last name, Job title, DOB (date of Birth), and the Email address (separately). Then...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • Add JavaScript scripts to the pizza order form to validate the formats of the phone number...

    Add JavaScript scripts to the pizza order form to validate the formats of the phone number and email. Validate the formats of the phone number and email when the previous validations have passed. Validate the phone number: If the phone number is not empty, validate that the phone number is in one of the following formats: ‘ddd-ddd-dddd’ – 10 digits with two dashes where the first dash is between the third and fourth digits and the second dash is between...

  • Please help this out with all Purpose: Learn how to get input and print out formatted...

    Please help this out with all Purpose: Learn how to get input and print out formatted results using scanf and printf separately in C. 1) Write a C program getPhone Number:c that accepts a phone number from the user in the form XXX-XXX-XXXX, and then displays it in the form (XXX)XXX-XXXX: Enter a phone (XXX-XXX-XXXX): 011-123-4567 You entered (011) 123-4567 Question: Execute your getPhoneNumber.c and attach a screenshot of the output. Then write the source code of getPhoneNumber.c in your...

  • ***** TO BE WRITTEN IN JAVA ***** Write an application that creates and prints a random...

    ***** TO BE WRITTEN IN JAVA ***** Write an application that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the output. Do not let the first three digits contain an 8 or 9 (but don't be more restrictive than that), and make sure that the second set of three digits is not greater than 742. Hint: Think through the easiest way to construct the phone number. Each digit does not have to be...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT