In: Computer Science
Write a C program
Your program will prompt the user to enter a value for the
amount of expenses on the credit card. Retrieve the user input
using fgets()/sscanf() and save the input value in a variable. The
value should be read as type double. The user may or may not enter
cents as part of the input. In other words, expect the user to
enter values such as 500, 500.00 and 500.10. The program will then
prompt the user to enter the Annual Percentage Rate (APR) for the
credit card. This value should also be read as type double and
saved in a second variable. Next, your program will prompt the user
to enter the number of days money is borrowed. This value should be
read as type int. After the values have been entered, your program
will calculate the amount of the interest and save the result in a
fourth variable. The program will then add the interest amount to
the expense amount and save this result in a fifth variable.
Finally, it will print all five variables (expense amount, APR,
number of days, amount of interest and total amount to be paid
back) to the screen in an informative format. Ensure that the
floating point output always displays exactly two decimal places.
Also, values of a magnitude less than 1 should display a leading 0
(ex. 0.75).
For interest calculation, we will use the compound interest method,
in which, we consider not only the interest on the actual borrowed
amount, but also the interest of interest that has already accrued.
We will use the following simple formula to compute compound
interest:
P' = P(1 +
r/n)^(nt)
where:
P is the original
principal sum
P' is the new
principal sum
r is the annual
interest rate
n is the
compounding frequency (we assume it to be 12, like most US
banks)
t is the overall
length of time the interest is applied (expressed in years).
The total compound interest generated is the final value minus the
initial principal:
I = P' - P
In order to compute the power function, you need to include math.h
library and call the pow() function within it. See the man page of
pow() to find more information about it. Also note that, the number
of days money is borrowed needs to be converted to number of years.
If you simply try days/365 to express it in terms of years, the
result of the division is likely going to be incorrect. The annual
interest rate, r, can be obtained from the annual percentage rate
(APR), using r = APR/100.
Your program should check to ensure that negative values are not
input. If an improper value is entered, an error message should be
printed and the program will exit (do not re-prompt the user for a
correct value).
For input, you must use the fgets()/sscanf() combination of
Standard I/O functions. Do not use the scanf() or fscanf()
functions. Although using scanf() for retrieving numerical data is
convenient, it is problematic when used in conjunction with
character string input (as you may discover if you use it in later
labs). Therefore, it is best to get into the habit of avoiding
scanf() completely, and using the fgets()/sscanf() combination
exclusively.
#include <stdio.h>
#include <math.h>
int main()
{
char inBuf[15]; // A buffer to hold the input from the
keyboard
double expense, arp, ci, ci_y, ci_d, intrest, rate;
int n_days, years, days;
printf("Enter value for the amount of expenses on the credit
card : ");
fgets(inBuf, 15, stdin); // Read the input from the keyboard and
store it in inBuf
sscanf(inBuf, "%lf", &expense); // Extract the double value
from inBuf and store it in expense
printf("Enter the Annual Percentage Rate (APR) for the credit
card : ");
fgets(inBuf, 15, stdin);
sscanf(inBuf, "%lf", &arp);
printf("Enter the number of days money is borrowed : ");
fgets(inBuf, 15, stdin);
sscanf(inBuf, "%d", &n_days);
if( expense < 0 || arp < 0 || n_days < 0){ // if any
entered value are negative then exit
printf("\nError...Improper value is entered");
return 0;
}
years = n_days / 365; // calculating number of years
days = n_days % 365; // calculating number of days
rate = arp/100;
ci_y = expense * pow((1 + rate / 12), 12*years); // CI for
years
ci_d = expense * pow((1 + rate / 365), days); // CI for days
ci = ci_y + ci_d; // Adding both CIs
intrest = ci - (2 * expense); // total compound interest
generated is the final value minus the 2 * initial principal
// one for years and another for days
printf("\nExpense Amount : %.2lf\n",expense);
printf("Annual Percentage Rate (APR) : %.2lf\n",arp);
printf("Number of days : %d\n",n_days);
printf("Amount of interest : %.2lf\n",intrest);
printf("Total amount to paid back : %.2lf\n",expense +
intrest);
return 0;
}
output