i have a problem with this code iv been try lots of expressions to show elven thousand to nineteen thousand and nothing seems to be can you guys help me out tnx in advance

#include<stdio.h>

int main()
{
int n,teens,thousands,hundreds,tens,ones,tthousands;

printf("Enter a number: ");
scanf("%d", &n);

tthousands=n/10000;
n=n%10000;
thousands=n/1000;
n=n%1000;
hundreds=n/100;
n=n%100;

if ((n>10) && (n<20))
{
tens=0;
ones=0;
teens=n%10;
}
else
{
tens=n/10;
n=n%10;
ones=n;
}
switch(tthousands)
{
case 1:printf("ten ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("forty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;

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

}
switch(hundreds)
{
case 1: printf("one hundred "); break;
case 2: printf("two hundred "); break;
case 3: printf("three hundred "); break;
case 4: printf("four hundred "); break;
case 5: printf("five hundred "); break;
case 6: printf("six hundred "); break;
case 7: printf("seven hundred "); break;
case 8: printf("eight hundred "); break;
case 9: printf("nine hundred "); break;
}
switch(teens)
{
case 1:printf("eleven ");break;
case 2:printf("twelve ");break;
case 3:printf("thirteen ");break;
case 4:printf("fourteen ");break;
case 5:printf("fifteen ");break;
case 6:printf("sixteen ");break;
case 7:printf("seventeen ");break;
case 8:printf("eighteen ");break;
case 9:printf("nineteen ");break;
}
switch(tens)
{
case 1:printf("ten ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("forty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}
switch(ones)
{
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 ");break;

}
getch();
return 0;
}

Recommended Answers

All 2 Replies

As a quick response, I would suggest initializing these local variables to zero.

int n,teens,thousands,hundreds,tens,ones,tthousands;

There is a bug in your program.

When you receive a number, you straight divide by 10,000 and the result is stored in an integer. So if the input is 12,123.... This is how your code will process it

tthousands= 12,123/10,000
= 1.212

this value will be stored as 1 as tthousands is an int variable.
What I would suggest is that if the number is between 10,000 and 20,000 your first divide by 1000 and then switch on the result. After that you can process for hundreds and so on

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.