Write a program that will prompt for and read a positive int

Write a program that will prompt for and read a positive integer less than 1000 from the keyboard, and then create and output a string that is the value of the integer in words.

For example, if 941 is entered, the program will create the string \"Nine hundred and forty one\".

USE THE DEFAULT TEMPLATE TO FINISH THE CODE DO NOT MODIFY THE TEMPLATE. WRITE THE CODE IN C LANGUAGE.

#include <stdio.h>
#include <string.h>

int main(void)
{
char *unit_words[] = {\"zero\", \"one\",\"two\",\"three\",\"four\",\"five\",\"six\",\"seven\",\"eight\",\"nine\"};
char *teen_words[] = {\"ten\", \"eleven\",\"twelve\",\"thirteen\",\"fourteen\",\"fifteen\",\"sixteen\",\"seventeen\",\"eighteen\",\"nineteen\"};
char *ten_words[] = {\"error\", \"error\",\"twenty\",\"thirty\",\"forty\",\"fifty\",\"sixty\",\"seventy\",\"eighty\",\"ninety\"};
char hundred[] = \" hundred\";
char and[] = \" and \";
char value_str[50] = \"\";
int value = 0; /* Integer to be converted */
int digits[] = {0,0,0}; /* Stores digits of value entered */
int i = 0;

printf(\"Enter a positive integer less than 1000: \");
scanf(\"%d\",&value);

// Your Code Here


return 0;
}

Solution

#include <stdio.h>
#include <string.h>
int main(void)
{
char *unit_words[] = {\"zero\", \"one\",\"two\",\"three\",\"four\",\"five\",\"six\",\"seven\",\"eight\",\"nine\"};
char *teen_words[] = {\"ten\", \"eleven\",\"twelve\",\"thirteen\",\"fourteen\",\"fifteen\",\"sixteen\",\"seventeen\",\"eighteen\",\"nineteen\"};
char *ten_words[] = {\"error\", \"error\",\"twenty\",\"thirty\",\"forty\",\"fifty\",\"sixty\",\"seventy\",\"eighty\",\"ninety\"};
char hundred[] = \" hundred\";
char and[] = \" and \";
char value_str[50] = \"\";
int value = 0; /* Integer to be converted */
int digits[] = {0,0,0}; /* Stores digits of value entered */
int i = 0,hat=0;
printf(\"Enter a positive integer less than 1000: \");
scanf(\"%d\",&value);
// Your Code Here
i=value;
digits[0]=i/100;
i=i%100;
digits[1]=i/10;
digits[2]=i%10;
if(value==0)
{
strcat(value_str,unit_words[0]);
}
if(digits[0]>=0)
{
i = digits[0];
value = value%100;
strcat(value_str,unit_words[i]);
strcat(value_str,hundred);
if(digits[1]>0 || digits[2]>0)
{
   hat=1;
   strcat(value_str,and);
}
}
if(digits[1]>=2)
{
//i = value/10;
//value = value%10;
strcat(value_str,ten_words[digits[1]]);
strcat(value_str,\" \");
}
else if(digits[1]==1)
{

   strcat(value_str,teen_words[value%10]);
}
if(digits[1]!=1 && digits[2]>0)
{
   strcat(value_str,unit_words[digits[2]]);
}
printf(\"%s\ \",value_str);
return 0;
}

Write a program that will prompt for and read a positive integer less than 1000 from the keyboard, and then create and output a string that is the value of the
Write a program that will prompt for and read a positive integer less than 1000 from the keyboard, and then create and output a string that is the value of the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site