C Program to Convert a Number into Words [Simple Method]

In this tutorial we will teach you how to write a C program to convert a given integer into string or words for example 23 will get converted into «Twenty Three».

C program to convert number into a string

Also read: /C-program-to-perform-implementation-of-Linked-List.html

So let’s start, The code for the above problem is given below:

#include<stdio.h> #include<string.h> int main(){ int len=0,x,one,ten,th,hundred; int no; char *ones[] = {"", "Zero", "One", "Two", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine"}; char *tens[] = {"", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen","Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; char *tens2[] = {"","","Ten","Twenty", "Thirty", "Forty", "Fifty","Sixty", "Seventy", "Eighty", "Ninety"}; printf("Enter a number:"); scanf("\%d",&no); x = no; while(no!=0){ no = no/10; len++; } if(len==1){ printf("Number in words is: \%s",ones[x+1]); } else if(len==2){ if(x>19){one = x\%10; ten = x/10; printf("Number in words is: \%s \%s",tens2[ten+1],ones[one+1]); } else{ one = x\%10; printf("Number in words is: \%s",tens[one+1]); } } else if(len==3){ hundred = x/100; ten = (x\%100)/10; one = ((x\%100)\%10); if(x\%100 > 10 && x\%100 < 20){ printf("Number in words is: \%s hundred \%s",ones[hundred+1],tens[one+1]); } else{ printf("Number in words is: \%s hundred \%s \%s",ones[hundred+1],tens2[ten+1],ones[one+1]); } } else if(len==4){ th = x/1000; hundred = (x\%1000) / 100; ten = ((x\%1000) \% 100)/10; one = (((x\%1000) \% 100)\%10); if(x\%1000 > 10 && x\%1000 < 20){ printf("Number in words is: \%s thousand \%s",ones[th+1],tens[one+1]); } else if(x\%1000>19 && x\%1000<100){ printf("Number in words is: \%s thousand \%s \%s",ones[th+1],tens2[ten+1],ones[one+1]); } else if(x\%1000 > 110 && x\%1000 < 120){ printf("Number in words is: \%s thousand \%s hundred \%s",ones[th+1], ones[hundred+1],tens[one+1]); } else{ printf("Number in words is: \%s thousand \%s hundred \%s \%s",ones[th+1],ones[hundred+1],tens2[ten+1],ones[one+1]); } } else{ printf("please enter a number below 10000"); } return 0; } 
Output

C program to convert number into string

Explaining the above code:

Note: The above program can only convert numbers below 10000.

In the above example, we declared three arrays which will hold the strings that we are going to use in converting the integer into a string. We make use of pointers because directly we cannot use strings as a data type in an array.

After that, we calculate the length of the integer using the while loop, for example, 23 will be of length ‘2’. Then using the if-else conditions we decide that on what length the string will use to be for example: if user entered 1 then the length will be one so the first condition will get executed and the compiler will pick the element from array ones[] and the element will be ones[x+1] where x=1 then the element will be ones[2] which are ‘one’.
This is how the above code is going to be executed.

Thanks for reading

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don’t forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.


C program to generate all combinations of N-Bit Binary String
10 July 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Macros in C with examples C Tutorials
30 January 2018

Armstrong Number program in C and C++
29 January 2018

Creating a Fixed Header Navigation Bar with on scroll effect
28 January 2018

C program to find Modular Multiplicative Inverse of two Relatively Prime Numbers
25 January 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий