Thread: count the letters in each number between 1 and 1000 inclusive

1. count the letters in each number between 1 and 1000 inclusive

im trying to count the letters in the words from 1 (one = 3) to 1000 ( one = 3 + thousand = 8 giving a total of 11)
Code:
```#include <stdio.h>
#include <stdlib.h>

#define HUNDRED_AND 10
#define HUNDREDS 7
#define THOUSAND 11
#define LIMIT 1000

int units(int units);
int tens(int tens);
int ten_to_nineteen(int units);
int count_letters(int number);

int main()
{
int i;
long long unsigned int letter_count = 0;

for (i = 1; i < LIMIT; i++)
{
letter_count += count_letters(i);
}

letter_count += THOUSAND; //and the letters for 1000 onto the total
printf("number of letters in all the numbers between 1 and 1000 inclusive are %llu", letter_count);

return 0;
}

int units(int units)
{
int result;

switch (units)
{
case 1:
result = 3; //3 letters in the word one.
break;
case 2:
result = 3;
break;
case 3:
result = 5;
break;
case 4:
result = 4;
break;
case 5:
result = 4;
break;
case 6:
result = 3;
break;
case 7:
result = 5;
break;
case 8:
result = 5;
break;
case 9:
result = 4;
}
return result;
}

int tens(int tens)
{
int result;

switch (tens) // deals with multiples of 10
{
case 2:
result = 6; //6 letters in the word twenty
break;
case 3:
result = 6;
break;
case 4:
result = 5;
break;
case 5:
result = 5;
break;
case 6:
result = 6;
break;
case 7:
result = 7;
break;
case 8:
result = 6;
break;
case 9:
result = 6;
}
return result;
}

int ten_to_nineteen(int units)
{
int result;

switch (units)
{
case 0:
result = 3; //ten
break;
case 1:
result = 6; //3 letters in the word eleven
break;
case 2:
result = 6;//twelve
break;
case 3:
result = 8;//thirteen
break;
case 4:
result = 8;//fourteen
break;
case 5:
result = 7; //fifteen
break;
case 6:
result = 7; //sixteen
break;
case 7:
result = 9;//seventeen
break;
case 8:
result = 8;//eighteen
break;
case 9:
result = 8;//nineteen
}
return result;
}

int count_letters(int number)
{
int unit, ten, hundred, total_letters_in_num = 0;

unit = number % 10;
ten = number % 100;
ten /= 10;
hundred = number / 100;

if (unit > 0)
{
total_letters_in_num = units(unit); //count the number of letters in the number of units.
}

if (ten > 0)
{
if (ten == 1)
{
total_letters_in_num += ten_to_nineteen(unit);
}
else
{
total_letters_in_num += tens((ten));//count the number of letters in the number of groups of 10
}
}

if (hundred > 0)
{
total_letters_in_num += units(hundred);
if (unit == 0 && ten == 0) //dealing with 100 200, 300 etc
{
total_letters_in_num += HUNDREDS;
}
else
{
total_letters_in_num += HUNDRED_AND;
}
}

}```
apparently the answer is 21124 and i get 21584 460 more.

if have tried altering the loop to check my functions and each time by my reckoning i get the right answer. ie i counted up to 9 and got 36. tried counting in 10's from 10 to 90 and got the right answer same with counting in hundreds and counting in hundreds with i = 101.

so i cant see why its not working
coop  Reply With Quote

In other words, start debugging.  Reply With Quote

3. if have tried altering the loop to check my functions and each time by my reckoning i get the right answer. ie i counted up to 9 and got 36. tried counting in 10's from 10 to 90 and got the right answer same with counting in hundreds and counting in hundreds with i = 101.  Reply With Quote

4. Code:
```#include <stdio.h>
#include <stdlib.h>

#define HUNDRED_AND 10
#define HUNDREDS 7
#define THOUSAND 11
#define LIMIT 1000

int units(int units);
int tens(int tens);
int ten_to_nineteen(int units);
int count_letters(int number);

int main()
{
int i;
long long unsigned int letter_count = 0;

for (i = 1; i < LIMIT; i++)
{
letter_count += count_letters(i);
}

letter_count += THOUSAND; //and the letters for 1000 onto the total
printf("number of letters in all the numbers between 1 and 1000 inclusive are %llu", letter_count);

return 0;
}

int units(int units)
{
int result;

switch (units)
{
case 1:
result = 3; //3 letters in the word one.
break;
case 2:
result = 3;
break;
case 3:
result = 5;
break;
case 4:
result = 4;
break;
case 5:
result = 4;
break;
case 6:
result = 3;
break;
case 7:
result = 5;
break;
case 8:
result = 5;
break;
case 9:
result = 4;
}
return result;
}

int tens(int tens)
{
int result;

switch (tens) // deals with multiples of 10
{
case 2:
result = 6; //6 letters in the word twenty
break;
case 3:
result = 6;
break;
case 4:
result = 5;
break;
case 5:
result = 5;
break;
case 6:
result = 6;
break;
case 7:
result = 7;
break;
case 8:
result = 6;
break;
case 9:
result = 6;
}
return result;
}

int ten_to_nineteen(int units)
{
int result;

switch (units)
{
case 0:
result = 3; //ten
break;
case 1:
result = 6; //3 letters in the word eleven
break;
case 2:
result = 6;//twelve
break;
case 3:
result = 8;//thirteen
break;
case 4:
result = 8;//fourteen
break;
case 5:
result = 7; //fifteen
break;
case 6:
result = 7; //sixteen
break;
case 7:
result = 9;//seventeen
break;
case 8:
result = 8;//eighteen
break;
case 9:
result = 8;//nineteen
}
return result;
}

int count_letters(int number)
{
int unit, ten, hundred, total_letters_in_num = 0;

unit = number % 10;
ten = number % 100;
ten /= 10;
hundred = number / 100;

if (unit > 0)
{
total_letters_in_num = units(unit); //count the number of letters in the number of units.
}

if (ten > 0)
{
if (ten == 1)
{
total_letters_in_num = 0;
total_letters_in_num += ten_to_nineteen(unit);
}
else
{
total_letters_in_num += tens((ten));//count the number of letters in the number of groups of 10
}
}

if (hundred > 0)
{
total_letters_in_num += units(hundred);
if (unit == 0 && ten == 0) //dealing with 100 200, 300 etc
{
total_letters_in_num += HUNDREDS;
}
else
{
total_letters_in_num += HUNDRED_AND;
}
}

}```
limit set to 10 i get the answer 36...correct
limit set to 20 i set to 10 i get the answer 70...correct
limit set to 100 i set to 10 i += 10 i get the answer 50... correct
limit set to 1000 i set to 100 i += 100 i get the answer 99 ...correct
limit set to 100 i set to 101 i += 100 i get the answer 153... correct

however

limit set to 1000 i set to 1 i++ i get the answer 21224 incorrect. should be 21584 360 out;  Reply With Quote

5. So keep exploring conditions to find what works and what doesn't.  Reply With Quote

6. Isn't it simpler code place more things in switches?
Default or case 0, case 1 when it's about tens returns result=0
Single-step in debugger,starting right before results get wrong  Reply With Quote

7. thats the thing...... each function works  Reply With Quote

8. Where do you get the "correct" answer from?

Consider using strings.
Code:
```int ten_to_nineteen(int units)
{
const char *word[] = {
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
};
//printf("%s",words[units]);
return strlen(words[units]);
}```
If each function is capable of printing out the string it thinks it is calculating the length for, then it becomes a lot easier to check what that code is actually doing.  Reply With Quote

9. look
0ne two three four five six seven eight nine = 36 same answer as post 1 and post 3
ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen = 70 same answer as post 3.
ten twenty thirty forty fifty sixty seventy eighty ninety = 50 same as post 3.
one hundred two hundred three hundred four hundred five hundred 6 hundred 7 hundred 8 hundred 9 hundred = 36 (1-9) + 9 * 7 (hundred = 7) = 99 same as post 3
one hundred and one two hundred and one three hundred and one four hundred and one five hundred and one six hundred and one seven hundred and one eight hundred and one nine hundred and one = 36(0-9) +9 * 10(hundred and) + 9 * 3 (one) = 36 + 90 + 27 = 153 same as post 3
so each function works correctly each one returns the right answer and writing it out here proves i have the spellings correct. so i am now even more stumped because that was the only other solution.  Reply With Quote

10. Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define HUNDRED_AND 10
#define HUNDREDS 7
#define THOUSAND 11
#define LIMIT 1000

int units(int units);
int tens(int tens);
int ten_to_nineteen(int units);
int count_letters(int number);

int main()
{
int i;
long long unsigned int letter_count = 0;

for (i = 101; i < LIMIT; i += 100)
{
letter_count += count_letters(i);
}

//letter_count += THOUSAND; //and the letters for 1000 onto the total
printf("number of letters in all the numbers between 1 and 1000 inclusive are %llu", letter_count);

return 0;
}

int units(int units)
{
/*
int result;

switch (units)
{
case 1:
result = 3; //3 letters in the word one.
break;
case 2:
result = 3;
break;
case 3:
result = 5;
break;
case 4:
result = 4;
break;
case 5:
result = 4;
break;
case 6:
result = 3;
break;
case 7:
result = 5;
break;
case 8:
result = 5;
break;
case 9:
result = 4;
}
return result;
//*/
char *single[] = {
"", "one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"};

printf("%s", single[units]);
return strlen(single[units]);
}

int tens(int tens)
{
/*
int result;

switch (tens) // deals with multiples of 10
{
case 2:
result = 6; //6 letters in the word twenty
break;
case 3:
result = 6;
break;
case 4:
result = 5;
break;
case 5:
result = 5;
break;
case 6:
result = 5;
break;
case 7:
result = 7;
break;
case 8:
result = 6;
break;
case 9:
result = 6;
}
return result;
//*/
char *multiples_ten[] = {
"",
"",
"twenty",
"thirty",
"fourty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"};

printf("%s\n", multiples_ten[tens]);
return strlen(multiples_ten[tens]);
}

int ten_to_nineteen(int units)
{
/*
int result;

switch (units)
{
case 0:
result = 3; //ten
break;
case 1:
result = 6; //3 letters in the word eleven
break;
case 2:
result = 6;//twelve
break;
case 3:
result = 8;//thirteen
break;
case 4:
result = 8;//fourteen
break;
case 5:
result = 7; //fifteen
break;
case 6:
result = 7; //sixteen
break;
case 7:
result = 9;//seventeen
break;
case 8:
result = 8;//eighteen
break;
case 9:
result = 8;//nineteen
}
return result;
//*/
char *teens[] = {
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"};

printf("%s\n", teens[units]);
return strlen(teens[units]);
}

int count_letters(int number)
{
int unit, ten, hundred, total_letters_in_num = 0;

unit = number % 10;
ten = number % 100;
ten /= 10;
hundred = number / 100;

if (unit > 0)
{
total_letters_in_num = units(unit); //count the number of letters in the number of units.
}

if (ten > 0)
{
if (ten == 1)
{
total_letters_in_num = 0;
total_letters_in_num += ten_to_nineteen(unit);
}
else
{
total_letters_in_num += tens((ten));//count the number of letters in the number of groups of 10
}
}

if (hundred > 0)
{
total_letters_in_num += units(hundred);
if (unit == 0 && ten == 0) //dealing with 100 200, 300 etc
{
printf(" hundred\n");
total_letters_in_num += HUNDREDS;
}
else
{
printf(" hundred and\n");
total_letters_in_num += HUNDRED_AND;
}
}

}```
i = 1 limit = 9 total = 36 same as post 1, 3 and 9
i = 10 limit = 20 total = 70 same as post 3 and post 9
i = 10 limit = 100 i += 10 total = 50 same as post 3 and 9
i = 100 limit = 1000 i += 100 total 99 same as post 3 and 9
i = 101 limit = 1000 i += 100 total 153 same as post 3 and 9

this is proving the definition of insanity repeating the same thing over and over expecting different results  Reply With Quote

11. You seem to be double-counting some numbers.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define HUNDRED_AND 10
#define HUNDREDS 7
#define THOUSAND 11
#define LIMIT 1000

int units(int units);
int tens(int tens);
int ten_to_nineteen(int units);
int count_letters(int number);

int main()
{
int i;
long long unsigned int letter_count = 0;

for (i = 101; i < 120; i += 1)
{
printf("N=%d, str=", i);
letter_count += count_letters(i);
printf(", total so far=%llu\n", letter_count);
}

//letter_count += THOUSAND; //and the letters for 1000 onto the total
printf("number of letters in all the numbers between 1 and 1000 inclusive are %llu", letter_count);

return 0;
}

int units(int units)
{
char *single[] = {
"", "one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"};

printf("%s", single[units]);
return strlen(single[units]);
}

int tens(int tens)
{
char *multiples_ten[] = {
"",
"",
"twenty",
"thirty",
"fourty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"};

printf("%s", multiples_ten[tens]);
return strlen(multiples_ten[tens]);
}

int ten_to_nineteen(int units)
{
char *teens[] = {
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"};

printf("%s", teens[units]);
return strlen(teens[units]);
}

int count_letters(int number)
{
int unit, ten, hundred, total_letters_in_num = 0;

unit = number % 10;
ten = number % 100;
ten /= 10;
hundred = number / 100;

if (unit > 0)
{
total_letters_in_num = units(unit); //count the number of letters in the number of units.
}

if (ten > 0)
{
if (ten == 1)
{
total_letters_in_num = 0;
total_letters_in_num += ten_to_nineteen(unit);
}
else
{
total_letters_in_num += tens((ten));//count the number of letters in the number of groups of 10
}
}

if (hundred > 0)
{
total_letters_in_num += units(hundred);
if (unit == 0 && ten == 0) //dealing with 100 200, 300 etc
{
printf("hundred");
total_letters_in_num += HUNDREDS;
}
else
{
printf("hundredand");
total_letters_in_num += HUNDRED_AND;
}
}

}```
Results
Code:
```\$ ./a.out
N=101, str=oneonehundredand, total so far=16
N=102, str=twoonehundredand, total so far=32
N=103, str=threeonehundredand, total so far=50
N=104, str=fouronehundredand, total so far=67
N=105, str=fiveonehundredand, total so far=84
N=106, str=sixonehundredand, total so far=100
N=107, str=sevenonehundredand, total so far=118
N=108, str=eightonehundredand, total so far=136
N=109, str=nineonehundredand, total so far=153
N=110, str=tenonehundredand, total so far=169
N=111, str=oneelevenonehundredand, total so far=188
N=112, str=twotwelveonehundredand, total so far=207
N=113, str=threethirteenonehundredand, total so far=228
N=114, str=fourfourteenonehundredand, total so far=249
N=115, str=fivefifteenonehundredand, total so far=269
N=116, str=sixsixteenonehundredand, total so far=289
N=117, str=sevenseventeenonehundredand, total so far=311
N=118, str=eighteighteenonehundredand, total so far=332
N=119, str=ninenineteenonehundredand, total so far=353
number of letters in all the numbers between 1 and 1000 inclusive are 353```
For example, all the teens also output (and thus count) the units as well.  Reply With Quote

12. thats why when i have a "teen number" i set the count to zero line 104  Reply With Quote

13. Originally Posted by cooper1200 thats why when i have a "teen number" i set the count to zero line 104
But it doesn't work does it.  Reply With Quote

14. I've found your error by adding another printf to your first code and setting the limit to something small (up to the number 20, so total 21) and then comparing it to hand calculations
Code:
```    for (i = 1; i < LIMIT; i++)
{
letter_count += count_letters(i);
printf("(%d) total = %d\n", i, letter_count);
}

// comment out this...
//letter_count += THOUSAND; //and the letters for 1000 onto the total

printf("number of letters in all the numbers between 1 and 1000 inclusive are %llu", letter_count);```
one ... twenty should be 112  Reply With Quote

15. @salem why doesn't it.... line 14 and 15 of your output are
N=113, str=threethirteenonehundredand, total so far=228
N=114, str=fourfourteenonehundredand, total so far=249
fourteen one hundred and = 21 letters
249 - 21 = 228

@ click here the number of letters for all the numbers from 1 to 19 is 106 ie 36 + 70 so as twenty is 6 letters that makes 112 and thats what i get
all i can assume is that the answer im trying to achieve is wrong.
i have checked and triple checked that i get the right answer. there cant be an issue with the switch statements as they have been done away with i get the correct output with the words. there is no reason what so ever that im still getting a different answer than the answer i found online.  Reply With Quote

Popular pages Recent additions break;, case, int, number, result 