C Number to Text problem with ones and tens..
Posted
by
Joegabb
on Stack Overflow
See other posts from Stack Overflow
or by Joegabb
Published on 2010-12-26T05:29:40Z
Indexed on
2010/12/26
5:54 UTC
Read the original article
Hit count: 381
#include<stdio.h>
#include<conio.h>
main()
{
int ones,tens,ventoteen, myloop = 0;
long num2,cents2,centeens,cents1,thousands,hundreds;
double num;
do{
printf("Enter a number: ");
scanf("%lf",&num);
if(num<=10000 || num>=0)
{
if (num==0)
{
printf("\t\tZero");
}
num=(num*100);
num2= (long)num;
thousands=num2/100000;
num2=num2%100000;
hundreds=num2/10000;
num2=num2%10000;
if ((num2>=1100) || (num2<=1900))
{
tens=0;
ones=0;
ventoteen=num2%1000;
}
else
{
tens=num2/1000;
num2=num2%1000;
ones=num2/100;
num2=num2%100;
}
if((num2>=11) && (num2<=19))
{
cents1=0;
cents2=0;
centeens=num2%10;
}
else
{
cents1=num2/10;
num2=num2%10;
cents2=num2/1;
}
if (thousands == 1)
printf("One thousand ");
else if (thousands == 2)
printf("Two thousand ");
else if (thousands == 3)
printf("Three Thousand ");
else if (thousands == 4)
printf("Four thousand ");
else if (thousands == 5)
printf("Five Thousand ");
else if (thousands == 6)
printf("Six thousand ");
else if (thousands == 7)
printf("Seven Thousand ");
else if (thousands == 8)
printf("Eight thousand ");
else if (thousands == 9)
printf("Nine Thousand ");
else
{}
if (hundreds == 1)
printf("one hundred ");
else if (hundreds == 2)
printf("two hundred ");
else if (hundreds == 3)
printf("three hundred ");
else if (hundreds == 4)
printf("four hundred ");
else if (hundreds == 5)
printf("five hundred ");
else if (hundreds == 6)
printf("six hundred ");
else if (hundreds == 7)
printf("seven hundred ");
else if (hundreds == 8)
printf("eight hundred ");
else if (hundreds == 9)
printf("nine hundred ");
else
{}
switch(ventoteen)
{
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;
}
switch(cents1)
{
case 1: printf("and ten centavos ");break;
case 2: printf("and twenty centavos ");break;
case 3: printf("and thirty centavos ");break;
case 4: printf("and fourty centavos ");break;
case 5: printf("and fifty centavos ");break;
case 6: printf("and sixty centavos ");break;
case 7: printf("and seventy centavos ");break;
case 8: printf("and eighty centavos ");break;
case 9: printf("and ninety centavos ");break;
}
switch(centeens)
{
case 1: printf("and eleven centavos ");break;
case 2: printf("and twelve centavos ");break;
case 3: printf("and thirteen centavos ");break;
case 4: printf("and fourteen centavos ");break;
case 5: printf("and fifteen centavos ");break;
case 6: printf("and sixteen centavos ");break;
case 7: printf("and seventeen centavos ");break;
case 8: printf("and eighteen centavos ");break;
case 9: printf("and nineteen centavos ");break;
}
switch(cents2)
{
case 1: printf("and one centavos ");break;
case 2: printf("and two centavos ");break;
case 3: printf("and three centavos ");break;
case 4: printf("and four centavos ");break;
case 5: printf("and five centavos ");break;
case 6: printf("and six centavos ");break;
case 7: printf("and seven centavos ");break;
case 8: printf("and eight centavos ");break;
case 9: printf("and nine centavos ");break;
}
}
getch();
}while(myloop == 0);
return 0;
}
my code is working fine but the problem is when i input 1 - 90 nothing appears but when i input 100 the output would be fine and that is "One Hundred" and so as 1000 the output would be "One Thousand". thanks for the help..
© Stack Overflow or respective owner