Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will prompt for and read a positive integer less than 1000

ID: 3779731 • Letter: W

Question

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;
}

Explanation / Answer

#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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote