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"))...

  • 2.29 Random Phone Number (Java) Write a program that creates and prints a random phone number...

    2.29 Random Phone Number (Java) Write a program that creates and prints a random phone number of the form 668-555-xxxx where xxxx is a 4-digit randomly generated number. Include the dashes in the output. Think through how you can guarantee a 4-digit number. Your output should look like: A random phone number: 678-555-1234 Given: import random number public class PhoneNumbers { //----------------------------------------------------------------- // Produces a random phone number of the form 678-555-xxxx. //----------------------------------------------------------------- } }

  • Hi I have to write this in C and wasn't sure how! These are two separate...

    Hi I have to write this in C and wasn't sure how! These are two separate codes to be done separately. Write a program that accepts data from the user in the form mm/dd/yyyy and then displays it in the form of yyyymmdd (see below). Enter a date (mm/dd/yyyy): 2/17/2011 You entered the data 20110217 Write a program that prompts the user to enter a telephone number in the form (xxx) xxx-xxxx and then displays the number in the format...

  • Write a program in Python to solve the following task: In this project, you will build...

    Write a program in Python to solve the following task: In this project, you will 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...

  • 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...

  • Write a program called telephone.java that prompts the user to enter their phone number using the...

    Write a program called telephone.java that prompts the user to enter their phone number using the specific format: (xxx) xxx-xxxx. The program should check whether the input entered is valid. The program must also check for the parenthesis () for the area code (the first three numbers). Example: (702)892-0516 would be correct but 702-892-0516 would not be correct. Example: Enter your phone number: 702-908-1333. 702-908-1333 is a NOT a valid phone number as there are no parenthesis (). Another example:...

  • 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...

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