Write a program in C that reads words from a text file. If the word is a number
ID: 3705532 • Letter: W
Question
Write a program in C that reads words from a text file. If the word is a number between 1 and 999, spells out it in English and write it to a new file; if the word is not a number, write it directly to the new file. For example: Original file: There are 453 people in this room. New file: There are four hundred fifty three people in this room.
Explanation / Answer
#include #include int main() { int hundreds = 0; int tens = 0; int int number; //input while(1) { printf("Enter a number between 1-999 (enter 0 to exit): "); scanf("%d", &number); if (number==0) break; hundreds = number / 100; tens = (number - (hundreds * 100)) / 10; - (hundreds * 100) - (tens * 10); if (hundreds > 0) { if (hundreds == 9) printf("Nine"); else if (hundreds == 8) printf("Eight"); else if (hundreds == 7) printf("Seven"); else if (hundreds == 6) printf("Six"); else if (hundreds == 5) printf("Five"); else if (hundreds == 4) printf("Four"); else if (hundreds == 3) printf("Three"); else if (hundreds == 2) printf("Two"); else printf("One"); printf(" Hundred "); //if (tens == 0 && 0) printf(" "); } if (tens != 1) { // we have a special case (teens) if (tens == 9) printf("Ninety "); else if (tens == 8) printf("Eighty "); else if (tens == 7) printf("Seventy "); else if (tens == 6) printf("Sixty "); else if (tens == 5) printf("Fifty "); else if (tens == 4) printf("Forty "); else if (tens == 3) printf("Thirty "); else if (tens == 2) printf("Twenty "); if (ones > 0) { if (ones == 9) printf("Nine "); else if (ones == 8) printf("Eight "); else if (ones == 7) printf("Seven "); else if (ones == 6) printf("Six "); else if (ones == 5) printf("Five "); else if (ones == 4) printf("Four "); else if (ones == 3) printf("Three "); else if (ones == 2) printf("Two "); else printf("One "); } else { printf(" "); } } else { if (ones > 0) { if (ones == 9) printf("Nineteen "); else if (ones == 8) printf("Eightteen "); else if (ones == 7) printf("Seventeen "); else if (ones == 6) printf("Sixteen "); else if (ones == 5) printf("Fifteen "); else if (ones == 4) printf("Fourteen "); else if (ones == 3) printf("Thirten "); else if (ones == 2) printf("Twelve "); else printf("Eleven "); } else { printf("Ten "); } } } return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.