Question

In: Computer Science

Write a C program Your program will prompt the user to enter a value for the...

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.

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); // 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


Related Solutions

IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in a number greater than or equal to zero and less than or equal to 100. If they do not you should alert them and end the program. Next, determine the letter grade associated with the number. For example, A is any grade between 90 and 100. Report the letter grade to the user.
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
Answer in JAVA Write a program that would prompt the user to enter an integer. The...
Answer in JAVA Write a program that would prompt the user to enter an integer. The program then would displays a table of squares and cubes from 1 to the value entered by the user. The program should prompt the user to continue if they wish. Name your class NumberPowers.java, add header and sample output as block comments and uploaded it to this link. Use these formulas for calculating squares and cubes are: square = x * x cube =...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
Create a C++ program which will prompt the user to enter a password continually until the...
Create a C++ program which will prompt the user to enter a password continually until the password passes the following tests. Password is 6 chars long Password has at least 1 number If the input does not match the tests, it will input a specific error message. "Pass must be 6 chars long." If the input is allowed, print a message saying "Correct Password Criteria."
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT