You are here:   HomeC Programming (Theory)Control Structures in CWrite a C program which to find the grace marks for a student using switch. The user should enter the class obtained by the student and the number of subjects he has failed in.

# C Program print a given integer number in words

by Dinesh Thakur Category: Control Structures

Consider that we wish to print a given positive integer number in words, as a sequence of digit strings. For example, number 123 should be printed as One Two Three. This might be required in financial applications, for example, to print the cheque amount in words.

We can use a loop to separate the digits of a given number and print each of them as a word using a switch statement. However, as the digits are separated from the right side (i. e., least significant digit) of a number, we need to reverse the digits of given number before this loop. The program segment that uses this approach is given below.

#include <stdio.h>

#include <math.h>

void main()

{

int rev,num;

clrscr();

printf("Enter the number ");

scanf("%d", &num);

/* reverse given number num */

rev = 0;

while(num > 0)

{

rev = rev * 10 + num % 10;

num /= 10;

}

/* print number as digit strings */

while(rev > 0)

{

switch (rev % 10)

{

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

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;

}

rev /= 10;

}

getch();

}

 Name E-mail

Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.

What's New and Popular

Popular Article

Search Content

Basic Courses