codescracker
c

C Program to Print Number in Words



« C Tutorial C Examples »

In this tutorial, we will learn about how to create a program in C that will ask from user to enter any number to convert that number in words (characters). Let's first create a program that will print any given one-digit number (by user at run-time) in words. For example, if user enters 7, then the program prints Seven as output. Later on, you will get the code about printing number (contains more than 1 digit) in words.

Print Single Digit Number in Word

// Print 1 digit Number in Word
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int num;
    printf("Enter the number (less than 10): ");
    scanf("%d", &num);
    printf("\n");
    if(num<10 && num>0)
    {
        if(num==1)
            printf("1=>One");
        else if(num==2)
            printf("2=>Two");
        else if(num==3)
            printf("3=>Three");
        else if(num==4)
            printf("4=>Four");
        else if(num==5)
            printf("5=>Five");
        else if(num==6)
            printf("6=>Six");
        else if(num==7)
            printf("7=>Seven");
        else if(num==8)
            printf("8=>Eight");
        else if(num==9)
            printf("9=>Nine");
    }
    else
        printf("Number must be in between 0 and 10");
    getch();
    return 0;
}

As the program was written under Code::Blocks IDE, therefore here is the sample run after successful build and run:

print number in words c

Let's supply any number under 10 say 8 and press ENTER key to see the output:

number in words under 10 c

Program Explained

Print Multiple Digit Number in Words

Let's create another program that prints any number (more than one-digit) by its digit-by-digit name (series of words). For example, if user enteres 8127, then program prints its output as Eight One Two Seven.

// Print Multiple digit Number in Words
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
	long int num, rev=0;
	int digit, rem;
	printf("Enter the number: ");
	scanf("%ld", &num);
	while(num>0)
	{
		rem = num%10;
		rev = rev*10 + rem;
		num = num/10;
	}
	while(rev>0)
	{
		digit = rev%10;
		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 ");
				break;
			case 0: printf("Zero ");
				break;
			default: printf("Something went wrong!!");
				break;
		}
		rev = rev/10;
	}
	getch();
	return 0;
}

Here is the sample run after successful build and run:

c program print number in words

Now supply any number say 8127 as input and press ENTER key. Here is the output you will see:

print number in words c program

Program Explained


« C Tutorial C Examples »