Please I need this assigment in C code (not c++). Preferable in editable (copy a
ID: 3810526 • Letter: P
Question
Please I need this assigment in C code (not c++). Preferable in editable (copy and paste) code.
Write a program that inputs a dollar amount to be printed on a check and then prints the amount in check-protected format with leading asterisks if necessary. Assume that nine spaces are available for printing an amount.
(Explanation) Suppose a paycheck contains nine blank spaces in which the computer is supposed to print the amount of a weekly paycheck. If the amount is large, then all nine of those spaces will be filled, for example:
On the other hand, if the amount is less than $1000, then several of the spaces will ordinarily be left blank—for example,
contains four blank spaces. If a check is printed with blank spaces, it’s easier for someone to alter the amount of the check. To prevent such alteration, many check-writing systems insert leading asterisks to protect the amount as follows:
Also, the program should also output the word equivalent of the amount. For example, the amount 52.43 should be written as
and the program should print out:
****52.43
FIFTY TWO AND 43/100.
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char getCharForInt(int n)
{
switch(n)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
}
}
int storeIntInCheckArray(char checkArray[], int value, int pos)
{
if (value == 0)
{
checkArray[pos--] = 0;
}
else
{
int i = 0;
while(value > 0 && pos >= 0)
{
i++;
checkArray[pos--] = getCharForInt(value%10);
value = value/10;
if (value > 0 && i % 3 == 0)
{
checkArray[pos--] = ',';
}
}
}
return pos;
}
/* A function that prints given number in words */
void print_num(int num)
{
long div,n1;
int flag,digit,pos,tot_dig;
if(num==0)
{
printf("Zero");
}
tot_dig = 0;
div = 1;
n1 = num;
while ( n1 > 9 )
{
n1 = n1 / 10;
div = div * 10;
tot_dig++;
}
tot_dig++;
pos = tot_dig;
while ( num != 0 )
{
digit= num / div;
num = num % div;
div = div / 10;
switch(pos)
{
case 2:
case 5: if ( digit == 1 )
flag = 1;
else
{
flag = 0;
switch(digit)
{
case 2: printf("TWENTY ");break;
case 3: printf("THIRTY ");break;
case 4: printf("FORTY ");break;
case 5: printf("FIFTY ");break;
case 6: printf("SIXTY ");break;
case 7: printf("SEVENTY ");break;
case 8: printf("EIGHTY ");break;
case 9: printf("NINTY ");
}
}
break;
case 1:
case 4: if ( flag == 1 )
{
flag = 0;
switch(digit)
{
case 0 : printf("TEN ");break;
case 1 : printf("ELEVEN ");break;
case 2 : printf("TWELVE ");break;
case 3 : printf("THIRTEEN ");break;
case 4 : printf("FOURTEEN ");break;
case 5 : printf("FIFTEEN ");break;
case 6 : printf("SIXTEEN ");break;
case 7 : printf("SEVENTEEN ");break;
case 8 : printf("EIGHTEEN ");break;
case 9 : printf("NINETEEN ");
}
}
else
{
switch(digit)
{
case 1 : printf("ONE ");break;
case 2 : printf("TWO ");break;
case 3 : printf("THREE ");break;
case 4 : printf("FIUR ");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 ");
}
}
if ( pos == 4 )
printf("THOUSAND ");
break;
case 3:
if ( digit> 0 )
{
switch(digit)
{
case 1 : printf("ONE ");break;
case 2 : printf("TWO ");break;
case 3 : printf("THREE ");break;
case 4 : printf("FIUR ");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 ");
}
printf("HUNDRED ");
}
break;
}
pos--;
}
if ( pos == 4 && flag == 0)
printf("THOUSAND");
else
if ( pos == 4 && flag == 1)
printf("TEN THOUSAND");
if ( pos == 1 && flag == 1 )
printf("TEN ");
}
int main()
{
double value;
printf("Enter check amount: ");
scanf("%lf", &value);
int val = value;
int fraction = (int)(value*100) % 100;
char checkArray[10];
checkArray[9]= '';
checkArray[8] = getCharForInt(fraction%10);
checkArray[7] = getCharForInt((fraction/10)%10);
checkArray[6] = '.';
int pos = storeIntInCheckArray(checkArray, val, 5);
int i;
for(i = 0; i <= pos; i++)
checkArray[i] = '*';
printf("%s ", checkArray);
print_num(val);
printf(" AND %d/100. ", fraction);
return 0;
}