0% found this document useful (0 votes)
34 views2 pages

File: /Home/Moris/Desktop/Numtowords/Main.C Page 1 of 2: /N /N %LD /N /N/N

The document contains code for a C program that takes a numeric input between 1 and 999 from the user and prints out the number in words. It uses a series of if/else statements and switches to convert each digit to its word equivalent based on its position, checking for special cases like "eleven" or "fifteen". Digits in the hundreds place are also checked separately from the tens and ones places. The program exits if an invalid number is entered.

Uploaded by

Moris FreeMan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

File: /Home/Moris/Desktop/Numtowords/Main.C Page 1 of 2: /N /N %LD /N /N/N

The document contains code for a C program that takes a numeric input between 1 and 999 from the user and prints out the number in words. It uses a series of if/else statements and switches to convert each digit to its word equivalent based on its position, checking for special cases like "eleven" or "fifteen". Digits in the hundreds place are also checked separately from the tens and ones places. The program exits if an invalid number is entered.

Uploaded by

Moris FreeMan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

File: /home/moris/Desktop/numToWords/main.

c Page 1 of 2

#include<stdlib.h>
#include<stdio.h>
int main() {
long num, div, n1;
int flag, digit, pos, dig;

printf("\nEnter a number between 1 and 999: \n");


scanf("%ld", &num);
printf("\nThe number you have entered is ");
if(num <= 0) {
printf("not within the required range, please enter a number between 1 and 999\n\n");
exit(0);
}

if(num > 999) {


printf("not within the required range, please enter a number between 1 and 999\n\n");
exit(0);
}

dig = 0;
div = 1;
n1 = num;

while ( n1 > 9 ) {
n1 = n1 / 10;
div = div * 10;

dig++;
}

dig++;
pos = dig;

while ( num != 0 ) {
digit = num / div;
num = num % div;
div = div / 10;
switch(pos) {
case 2:
case 5:
if ( digit == 1 )
flag = 1;
else {
flag = 0;
switch(digit) {
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("ninty ");
}
}
break;
case 1:
case 4:
if (flag == 1) {
flag = 0;
switch(digit) {
File: /home/moris/Desktop/numToWords/main.c Page 2 of 2

case 0 : printf("ten ");break;


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 ");
}
} else {
switch(digit) {
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 ");
}
}

if (pos == 4)
printf("thousand ");
break;

case 3:
if (digit > 0) {
switch(digit) {
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 ");
}
printf("hundred ");
}
break;
}
pos--;
}
if (pos == 4 && flag == 0)
printf("thousand");
else if (pos == 4 && flag == 1)
printf("ten thousand");

if (pos == 1 && flag == 1)


printf("ten ");
}

You might also like