Design an algorithm and draw corresponding flowchart to print the value of the number in words when the number entered is in the range of 1 to 299. 10m Dec2006

By | June 25, 2014

Design an algorithm and draw corresponding flowchart to print the value of the number in words when the number entered is in the range of 1 to 299. 10m Dec2006

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char num[3];
void convert_to_words(char*);
clrscr();
printf(“Enter any number between 1 to 299 : “);
scanf(“%s”,&num);
convert_to_words(num);
getch();
}
void convert_to_words(char *num)
{
char *single_digits[] = { “zero”, “one”, “two”, “three”, “four”,
“five”, “six”, “seven”, “eight”, “nine”};

char *two_digits[] = {“”, “ten”, “eleven”, “twelve”, “thirteen”, “fourteen”,
“fifteen”, “sixteen”, “seventeen”, “eighteen”, “nineteen”};

char *tens_multiple[] = {“”, “”, “twenty”, “thirty”, “forty”, “fifty”,
“sixty”, “seventy”, “eighty”, “ninety”};
int len = strlen(num);
printf(“\n%s: “, num);
if (len == 1)
{
printf(“%s\n”, single_digits[*num - '0']);
return;
}
while (*num != ‘\0′)
{
if (len == 3)
{
if (*num -’0′ != 0)
{
printf(“%s “, single_digits[*num - '0']);
printf(“hundred “);
}
–len;
}
else
{
if (*num == ’1′)
{
int sum = *num – ’0′ + *(num + 1)- ’0′;
printf(“%s\n”, two_digits[sum]);
return;
}
else if (*num == ’2′ && *(num + 1) == ’0′)
{
printf(“twenty\n”);
return;
}
else
{
int i = *num – ’0′;
printf(“%s “, i? tens_multiple[i]: “”);
++num;
if (*num != ’0′)
printf(“%s “, single_digits[*num - '0']);
}
}
++num;
}
}

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char num[3];
void convert_to_words(char*);
clrscr();
printf("Enter any number between 1 to 299 : ");
scanf("%s",&num);
convert_to_words(num);
getch();
}
void convert_to_words(char *num)
{
char *single_digits[] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
 
char *two_digits[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
 
char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
int len = strlen(num);
printf("\n%s: ", num);
if (len == 1)
{
printf("%s\n", single_digits[*num - '0']);
return;
}
while (*num != '\0')
{
if (len == 3)
{
if (*num -'0' != 0)
{
printf("%s ", single_digits[*num - '0']);
printf("hundred ");
}
--len;
}
else
{
if (*num == '1')
{
int sum = *num - '0' + *(num + 1)- '0';
printf("%s\n", two_digits[sum]);
return;
}
else if (*num == '2' && *(num + 1) == '0')
{
printf("twenty\n");
return;
}
else
{
int i = *num - '0';
printf("%s ", i? tens_multiple[i]: "");
++num;
if (*num != '0')
printf("%s ", single_digits[*num - '0']);
}
}
++num;
}
}

Screen Shots:

C_program_Number_2_Words

 

C_program_Number_2_Words_Output

 

Leave a Reply