Question

In: Computer Science

Description: The purpose of this assignment is to write code that calculates the amount of interest...

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:

  • Read the man pages for fgets(), sscanf(), and printf() noting how format strings are used in each function. Pay careful attention to how floating point values are printed with printf() and how to create a reference to a variable when using sscanf() (use the & before the variable that is to contain the input).
  • Read the man page for pow().
  • 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.

    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.

Solutions

Expert Solution

#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 :


Related Solutions

In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will ask the user for their name; the length and width of a rectangle; and the length of a square. The program will then output the input name; the area and perimeter of a rectangle with the dimensions they input; and the area and perimeter of a square with the length they input. Tasks The program needs to contain the following A comment header containing...
Python 3: Write a function called Interest with floating point parameters Amount and InterestPercent that calculates...
Python 3: Write a function called Interest with floating point parameters Amount and InterestPercent that calculates the simple interest on Amount using InterestPercent as the interest rate.The function should return the interest amount.
Modify the provided code to create a program that calculates the amount of change given to...
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount. Use three functions: • bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false. • float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal,...
Description The purpose of this assignment is to demonstrate understanding of basic alterations in vision and...
Description The purpose of this assignment is to demonstrate understanding of basic alterations in vision and hearing. Compose a short (1.5 – 2.5 page) essay explaining one common alteration in vision and one common alteration in hearing. You should briefly describe the pathophysiologic changes, prevalence, manifestations, and treatment for each. You should also explain the mechanism that the specific treatment uses to return a normal functioning state. You may select a disease process from the book, but will need to...
AS2 Description: The purpose of this assignment is to learn to compute key numerical measures for...
AS2 Description: The purpose of this assignment is to learn to compute key numerical measures for descriptive statistics. AS2 Instructions: 1. The following data set is about the number of times a sample of 20 families dined out last week: 6 1 5 3 7 3 0 3 1 3 4 1 2 4 1 0 5 6 3 1 2. Compute the mean and median. 3. Compute the first and third quartiles. 4. Compute the variance and standard deviation....
Your task is to write a program in C or C++ that calculates the total amount...
Your task is to write a program in C or C++ that calculates the total amount of money a person has made over the last year. Your program will prompt the user for dollar amounts showing how much the user has made per job. Some users will have had more than one job, make sure your program allows for this. The program will print out the total made (all jobs) and will print out the federal and state taxes based...
Write a function that implements the following formula to calculates the amount of financial assistance for...
Write a function that implements the following formula to calculates the amount of financial assistance for families in need: • If the annual family income is between $30,000 and $40,000 and the family has at least 3 children, the assistance amount will be $1,000 per child. • If the annual family income is between $20,000 and $30,000 and the family has at least 2 children, the assistance amount will be $1,500 per child. • If the annual family income is...
The purpose of this assignment is to gain a better understanding of the code sets used...
The purpose of this assignment is to gain a better understanding of the code sets used for medical billing. These sets can be complicated, but you will learn in the EHR#8 assignment this week, that the EHR practice management functions for billing can use the clinical information to help you with code selection. Coding Classification Sets for Medical Coding and Billing Code Set When is this Code Set used? Format Example Source CPT Category I CPT Category I codes are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT