In: Computer Science
Description:
The purpose of this assignment is to write code that calculates the amount of interest accrued on a credit card on its expenses. You will create a C program that prompts the user for the total amount of purchases on the credit card, an annual percentage rate (APR), and the number of days the money is borrowed. The program will calculate the amount of the compound interest for the purchases, then print the original amount of purchases, the amount of the interest, and the total amount needed to be paid back.
Preparation:
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.
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.
Although you will learn more about this in later lectures and labs,
this is what you should typically do to read a number using the
fgets()/sscanf() combination:
// in main() or some function
char inBuf[20]; // A buffer to hold the input from the
keyboard
int someInt = 0; // A variable to hold the number from the
keyboard
fgets(inBuf, 20, stdin); // Read the input from the keyboard and
store it in inBuf
sscanf(inBuf, "%d", &someInt); // Extract the numerical value
from inBuf and store it in someInt
Note from the sscanf() manpage that when reading a floating point value, if the data type of the variable that will receive the value is of type double, you must use "%lf" (long float) instead of "%f" (float) for your format string.
#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);
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 :