#include <stdio.h>
 
int getNumber();
int checkSize(int i);
int negative(int i);
int thousands(int i);
int hunderds(int i);
void tens(int i);
void writeTens(int i);
void writeLowerTens(int i);
 
void writeNum(int i);
 
int main() {
	int iNum = getNumber();
	if (checkSize(iNum)) return 0;
	printf("Using: %d\n",iNum);
	iNum = negative(iNum);
	if (iNum > 999) iNum = thousands(iNum);
	if (iNum > 99) iNum = hunderds(iNum);
	tens(iNum);
	printf("\n");
	return 0;
}
 
int getNumber() {
	char text[10];
	fputs("Enter number: ", stdout);
	fflush(stdout);
	if (fgets(text, sizeof text, stdin)) {
		int number;
		if (sscanf(text, "%d", &number)) {
			return number;
		}
	}
	return 0;
}
 
int checkSize(int i) {
	if (i > 9999 || i < -9999) {
		printf("Number is outof range!\n");
		return(1);
	} else return 0;
}
 
int negative(int i) {
	if (i < 0) {
		printf("negative ");
		return (i - (i*2));
	}
}
 
void writeNum(int i) {
	switch(i){
		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;
		case 0:
			printf("zero");
			break;
		default:
			printf("fail!");
	}
}
 
int thousands(int i) {
	int thou = (i / 1000);
	writeNum(thou);
	printf(" thousand ");
	return (i - (thou * 1000));
 
}
int hunderds(int i) {
	int hund = (i / 100);
	writeNum(hund);
	printf(" hunderd and ");
	return (i - (hund * 100));
}
 
void tens(int i) {
	if (i > 9) {
		int tens = (i / 10);
		if(tens == 1) writeLowerTens(i);
		else {
			writeTens(tens);
			i = i - (tens * 10);
			writeNum(i);
		}
	} else writeNum(i);
}
 
void writeTens(int i){
	switch(i) {
		case 2:
			printf("twenty");
			break;
		case 3:
			printf("thirty");
			break;
		case 4:
			printf("fourty");
			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;
		default:
			printf("\nThis should never happen.\n");
	}
}
 
// This range is irregular so it needs to be handled seperately
void writeLowerTens(int i){
	switch(i) {
		case 10:
			printf("ten");
			break;
		case 11:
			printf("eleven");
			break;
		case 12:
			printf("twelve");
			break;
		case 13:
			printf("thirteen");
			break;
		case 14:
			printf("fourteen");
			break;
		case 15:
			printf("fifteen");
			break;
		case 16:
			printf("sixteen");
			break;
		case 17:
			printf("seventeen");
			break;
		case 18:
			printf("eighteen");
			break;
		case 19:
			printf("nineteen");
			break;
		default:
			printf("\nThis should never happen.\n");
	}
}