So far i managed to get up to 99 with my code. i need to get up to 9999 and it should be able to display as a string for example you input 345 it comes out "three hundred forty five"
#include <iostream>
#include <string>
using namespace std;
please use code tags
try using the cstring header file and use strlen() to get how many characters are in the string. then based on how many numbers there are you just have to have to add the terms. i reccomend using a switch statement to get through each individual number
#include <stdio.h>
int getnthterm(int num, int n)
{
int x = 1, y, z = 0;
while (z < n) {
y = x;
x *= 10;
++z;
}
if (y > num) return -999; /* error */
return (num % x) / y;
}
int main()
{
char *b[9][9] = {
{ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" },
{ "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }
};
int num, x, y;
printf("Enter number: ");
scanf("%d", &num);
x = 10;
while (x > 0) {
y = getnthterm(num, x);
if (y != 0 && y != -999) {
if (x == 3) printf ("%s hundred ", b[0][y-1]);
elseif (x == 4) printf("%s thousand ", b[0][y-1]);
else
printf("%s ", b[x-1][y-1]);
}
--x;
}
putchar('\n');
return 0;
}
Yeah, it's in C so u don't copy paste and cheat (it has a bug too). It'll only print upto 9999 but it's better structured has loops.
You should try something similar, in a neat way.
First write it down on a paper, think mathematically. I repeat, think mathematically. First do it on paper.
Good luck.
I am not looking for a differnet way to do this. Im trying to see how can I display the hundreds and the thousands like i did with the tens. Using the same code i have there