Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a program that takes any number in the range 0 to 999999999999 (1 less than a trillion) and writes it out in words using the words hundred, thousand, million, billion and the standard words for numbers under 100. Here are some examples:
8: eight; 39: thirty nine; 542: Five hundred and forty two; 8769: Eight thousand seven hundred sixty nine;
174345: One hundred seventy four thousand three hundred forty five; 6457892: 6 million four hundred fifty
seven thousand eight hundred ninety two; 999999876543: Nine hundred ninety nine billion nine hundred ninety
nine million eight seventy six thousand five hundred forty three. [50]

What I have tried:

C++
#include <stdio.h>

void print_num(int num);
void case1(int num);
void case2(int num);
void case3(int num);

int main()
{
    long num;
    int ones,thousands,millions,billions;
    printf("Enter the number:");
    scanf("%ld",&num);

    billions=num/1000000000;
    num=num%1000000000;
    millions=num/1000000;
    num=num%1000000;
    thousands=num/1000;
    ones=num%1000;
    printf("The number in word format is:\n");
    if(num==0){
        printf("Zero\n");
        return(0);

    }
    if(billions!=0)
    {
        print_num(billions);
        printf("Billion");

    }
    if(millions!=0){
         print_num(millions);
        printf("million");
    }
    if(thousands!=0)
    {
        print_num(thousands);
        printf("thousand");
    }
    print_num(ones);
    printf("\n");
    return(0);
}
void print_num(int num){
    int hundred=0,ten=0,one=0,tens=0;
    hundred=num/100;
    num=num%100;
    ten=num/10;
    if(ten==1){
        tens=num;
        one=num%10;
    }
    if (hundred!=0){
        case1(hundred);
        printf("hundred");
    }
    if(ten==1){
        case3(tens);
        return;
    }
    if(ten!=0){
        case2(ten);
    }
    if(one!=0){
      case1(one);
    }
}

void case1(int num)
{
    switch(num)
    {
        case 1:printf("One");
        break;
        case 2:printf("Two");
        break;
        case 3:printf("Three");
        break;
        case 4:printf("Four");
        break;
        case 5:printf("Five");
        break;
        case 6:printf("Six");
        break;
        case 7:printf("Seven");
        break;
        case 8:printf("Eight");
        break;
        case 9:printf("Nine");


    }
}
void case2(int num)
{
    switch(num)
    {
        case 1:printf("Ten");
        break;
        case 2:printf("Twenty");
        break;
        case 3:printf("Thirty");
        break;
        case 4:printf("Fourty");
        break;
        case 5:printf("Fivty");
        break;
        case 6:printf("Sixty");
        break;
        case 7:printf("Seventy");
        break;
        case 8:printf("Eighty");
        break;
        case 9:printf("Ninety");


    }
}
void case3(int num)
{
    switch(num)
    {
        case 1:printf("Eleven");
        break;
        case 2:printf("Twelve");
        break;
        case 3:printf("Thirteen");
        break;
        case 4:printf("Fourteen");
        break;
        case 5:printf("Fivteen");
        break;
        case 6:printf("Sixteen");
        break;
        case 7:printf("Seventeen");
        break;
        case 8:printf("Eighteen");
        break;
        case 9:printf("Nineteen");


    }
}

I dont know why my code is giving wrong output..
Posted
Updated 24-Sep-21 3:18am
v4
Comments
Richard Deeming 24-Sep-21 8:24am    
"I want" is not a question.

If you have a specific question about your code, the click the green "Improve question" link and update your question to provide a clear and complete description of the problem you have, what you have tried, and where you are stuck.

If your question is "do my homework for me", then you've come to the wrong site. Nobody here is going to do your work for you.

If you genuinely don't know where to start, then talk to your teacher.
ravi sharma 3456 24-Sep-21 8:31am    
I had clearly wrote what I tried,there are errors in that which i wanted the community to rectify
Richard Deeming 24-Sep-21 8:33am    
No you haven't. You've posted your homework assignment and an unformatted block of unexplained code.

Update your question to explain precisely where you are stuck.
ravi sharma 3456 24-Sep-21 8:36am    
its giving wrong output and i dont know why
Patrice T 24-Sep-21 8:50am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Feel free to add samples of what give wrong outputs.

An easier method of getting the words is to use arrays like:
C++
const char* units[] = { "null", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
                "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen"
                "eighteen", "nineteen" };
const char* tens[] = { "null", "null", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

// You can now index those lists for any number after you have found its base count.

int number = 137;
int base = number / 100; // base = 1 hundred
if (base > 0)
    print(units[base]);
number %= 100;   // number = 37
base = number / 10; // base = 3 tens
if (base > 0)
    print(tens[base]);
base = number % 10;   // number = 7
if (number > 0)
    print(units[base]);
cout << endl;
 
Share this answer
 
Comments
merano99 24-Sep-21 18:49pm    
The approach is correct, but something goes wrong in the lower part.
It could possibly go like this.

// find base tens
if (number > 19) {
base = number / 10; // base = 3 tens
cout << tens[base] << " ";
number = number % 10; // number = 7
}
// find units up to 19
if (number > 0)
cout << units[number];
Richard MacCutchan 25-Sep-21 3:16am    
Thanks, well spotted. I must admit I did not test it to destruction. :(
You're going to have to do this yourself. Nobody is going to write your code for you.

The point behind the assignment is to get you to think about the problem. It's NOT to make other people think about your problem and solve it for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900