1

I have this code that will print numbers from 0 to 9 in english words (like one for 1, two for 2, etc.). What if I wanted to print 374? Or something much larger, like 7549846451?

#include <stdio.h>
    int main() 
    {
        double sum;
        if(scanf("%1f",&num)!=0)
        {
            if(num=(int)num)
            {
                switch((int)sum)
                {
                    case 0:printf("zero\n");break;
                    case 1:printf("one\n");break;
                    case 2:printf("two\n");break;
                    case 3:printf("three\n");break;
                    case 4:printf("four\n");break;
                    case 5:printf("five\n");break;
                    case 6:printf("six\n");break;
                    case 7:printf("seven\n");break;
                    case 8:printf("eight\n");break;
                    case 9:printf("nine\n");break;
                    default:printf("not a digit"); break;
                }
            }else
            {
                printf("Invalid")
                    return 0;
            }
        }
        return 0;
    }
Kevin Thomson
  • 69
  • 1
  • 2
  • 6
  • 2
    `What if I wanted to print 374`... You have to write code for that. Period. – Sourav Ghosh Jun 23 '16 at 13:53
  • you can do that with some arrays, in which if the first digit is 1 and it's a 3 digit number, it prints first element of the first array which is "one hundred", if the second digit is 4 for example, it prints 4th element of the second array which is "forty" and so on – ReshaD Jun 23 '16 at 13:54
  • If the code only works with `int`egers, why not change `sum` to an `int` and `scanf` it with `%d`? – Kninnug Jun 23 '16 at 13:54
  • @Kevin Thomson What is the meaning of two variables sum and num? – Vlad from Moscow Jun 23 '16 at 13:55
  • There's several examples of how to write such programs if you search on SO. – Lundin Jun 23 '16 at 13:58
  • `num` is not declared anywhere? What type is `num`? – chux - Reinstate Monica Jun 23 '16 at 14:00
  • Just Google it first my friend before posting here :) – Stacked Jun 23 '16 at 14:33
  • This kind of question reappears every now and then on SO. There used to be one, a week ago. Please, read [this](http://stackoverflow.com/questions/9655202/how-to-convert-integer-to-string-in-c), or [this](http://stackoverflow.com/questions/11819837/converting-integer-to-string-in-c-without-sprintf), or [this](http://stackoverflow.com/questions/8257714/how-to-convert-an-int-to-string-in-c). – user3078414 Jun 23 '16 at 15:30

2 Answers2

6

This is a good start, but it would take a lot more to complete your program:

  • Start by expanding your code to printing numbers 10..99. There would be a special case for 11..19, but after that it's pretty regular. The lower 20 can be addressed with a lookup table. In fact, making a look-up table for the whole range wouldn't be too bad, either.
  • With a routine that writes out numbers 0..99 in hand you can expand into hundreds by looking at the third digit the right, writing it out, adding "hundred", and proceeding to writing out the number 0..99
  • Now that you have a routine for writing out three-digit numbers all you need is to split your number into groups of tree, calling this routine for non-zero groups, and adding "billion", "million", and "thousand" corresponding to the rank of the group.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Here you have the solution to your problem. It is even the same example as you have pasted here, so if you have read the comments below, you'd have seen the comment form Bheema in which he posted the whole code for it.

Also, you can try writing your own code, it's not that hard. dasblinkenlight gave you instructions how to do it.

Community
  • 1
  • 1
Mirakurun
  • 4,859
  • 5
  • 16
  • 32