I would like overpay to display overpay as the total amount paid if the result was not rounded. calculateMonths must be an integer which makes this much more difficult. I tried using
double dblCalculateMonths = (double)calculateMonths(principal, annualInterestRate, monthlyPayment);
but it did not work properly. I am not sure how to fix this problem while keeping calculateMonths as an integer. Help would be very much appreciated!
I would like the results to look like this:
** Welcome to the CPSC 1010-S3 Payment Calculator **
Enter the principal amount: 5000.00
Enter the annual interest rate (in %): 15.0
Enter the monthly payment: 100.00
Calculating...
Total # of months needed to pay off: 79
App roximate # of years needed to pay off: 6.6
Total interest paid: $2900.00
Total amount paid: $7900.00
You overpaid: $4.43
But the last line displays as
You overpaid: $100.00
I am not sure how to change this. My list of code is below
#include <stdio.h>
#include <math.h>
int calculateMonths(double principal,double annualInterestRate,double monthlyPayment)
{
double x = ((log(monthlyPayment)-log(monthlyPayment-annualInterestRate/1200.0*principal))/log(annualInterestRate/1200.0+1.0));
return x;
}
int main()
{
double principal, annualInterestRate, monthlyPayment;
printf("** Welcome to the Payment Calculator **\n\n");
printf("Enter the principal amount:\t\t\t");
scanf("%lf", &principal);
printf("Enter the annual interest rate (in%%):\t\t");
scanf("%lf", &annualInterestRate);
printf("Enter the monthly payment:\t\t\t");
scanf("%lf", &monthlyPayment);
printf("\nCalculating...\n\n");
double roundedMonths = calculateMonths(principal, annualInterestRate, monthlyPayment)+1;
printf("Total # of months needed to pay off:\t\t %.0f\n", roundedMonths);
double years = roundedMonths/12;
printf("Approcimate # of years needed to pay off: \t %.1f\n", years);
double amountPaid = roundedMonths * monthlyPayment;
double interestPaid = amountPaid-principal;
printf("Total interest paid:\t\t\t\t $%.2f\n", interestPaid);
printf("Total amount paid:\t\t\t\t $%.2f\n", amountPaid);
double dblCalculateMonths = (double)calculateMonths(principal, annualInterestRate, monthlyPayment);
double overpay = amountPaid - dblCalculateMonths*monthlyPayment;
printf("You overpaid:\t\t\t\t $%.2f\n", overpay);
printf("%f", dblCalculateMonths);
printf("\n\n");
return(0);
}
what's the logic in keeping calculateMonths() an integer? make it a double and it should work.
double calculateMonths(double principal,double annualInterestRate,double monthlyPayment)
and make roundedMonths an integer. Also you could add 0.5 to have the right round.
int roundedMonths = calculateMonths(principal, annualInterestRate, monthlyPayment)+0.5;
printf("Total # of months needed to pay off:\t\t %d\n", roundedMonths);
LE: Try this:
int calculateMonths(double principal,double annualInterestRate,double monthlyPayment, double *dblCalculateMonths)
{
*dblCalculateMonths = ((log(monthlyPayment)-log(monthlyPayment-annualInterestRate/1200.0*principal))/log(annualInterestRate/1200.0+1.0));
return *dblCalculateMonths;
}
int main()
{
double principal, annualInterestRate, monthlyPayment, dblCalculateMonths;
printf("** Welcome to the Payment Calculator **\n\n");
printf("Enter the principal amount:\t\t\t");
scanf("%lf", &principal);
printf("Enter the annual interest rate (in%%):\t\t");
scanf("%lf", &annualInterestRate);
printf("Enter the monthly payment:\t\t\t");
scanf("%lf", &monthlyPayment);
printf("\nCalculating...\n\n");
double roundedMonths = calculateMonths(principal, annualInterestRate, monthlyPayment, &dblCalculateMonths)+1;
printf("Total # of months needed to pay off:\t\t %.0f\n", roundedMonths);
double years = roundedMonths/12;
printf("Approcimate # of years needed to pay off: \t %.1f\n", years);
double amountPaid = roundedMonths * monthlyPayment;
double interestPaid = amountPaid-principal;
printf("Total interest paid:\t\t\t\t $%.2f\n", interestPaid);
printf("Total amount paid:\t\t\t\t $%.2f\n", amountPaid);
double overpay = amountPaid - dblCalculateMonths*monthlyPayment;
printf("You overpaid:\t\t\t\t $%.2f\n", overpay);
printf("%f", dblCalculateMonths);
printf("\n\n");
return(0);
}
Related
I'm trying to add tax to a number and after compiling the tax is not included in the addition to the number.
I have tried int or float also this %f but it seems it is not making the math after compiling.
#include <stdio.h>
int main()
{
float setprice, price, tax;
printf("enter price in dollars : \n");
scanf("%f", &price);
printf("Enter the tax: \n");
scanf("%f", &tax);
setprice=(price*tax)+price;
printf("total = %f\n", price);
return 0;
}
After entering the code on IDE this is the output.
enter price in dollars :
100
Enter the tax:
50
total = 100.000000
Your problem is adding "%" to tax variable. when you enter 50 it should be like 50%.
Then do this
tax = tax / 100
setprice = (price * tax) + price;
printf("total = %f\n", setprice);
or you can try this
setprice = (price * (tax / 100)) + price;
printf("total = %f\n", setprice);
these answers will work!
I would like my program to have a yes no prompt to ask if the program should be run again. Right now, it skips the user input completely. How can I do this effectively?
Any help would be appreciated. Thank you!
#include <stdio.h>
#include <math.h>
double Months(double principal,double annualInterestRate,double monthlyPayment)
{
double x = ((log(monthlyPayment)-log(monthlyPayment-annualInterestRate/1200.0*principal))/log(annualInterestRate/1200.0+1.0));
return x;
}
int calculateMonths(double y)
{
double principal, annualInterestRate, monthlyPayment;
y = Months(principal, annualInterestRate, monthlyPayment);
return y;
}
int main()
{
double principal, annualInterestRate, monthlyPayment;
char response = 'Y';
printf("** Welcome to the CPSC 1010-S3 Payment Calculator **\n\n");
do{
printf("Enter the principal amount:\t\t\t");
scanf("%lf", &principal);
printf("Enter the annual interest rate (in%%):\t\t");
scanf("%lf", &annualInterestRate);
printf("Enter the monthly payment:\t\t\t");
scanf("%lf", &monthlyPayment);
printf("\nCalculating...\n\n");
int roundedMonths = Months(principal, annualInterestRate, monthlyPayment)+.5;
printf("Total # of months needed to pay off:\t\t %d\n", roundedMonths);
double years = Months(principal, annualInterestRate, monthlyPayment)/12;
printf("Approcimate # of years needed to pay off: \t %.1f\n", years);
double amountPaid = roundedMonths * monthlyPayment;
double interestPaid = amountPaid-principal;
printf("Total interest paid:\t\t\t\t $%.2f\n", interestPaid);
printf("Total amount paid:\t\t\t\t $%.2f\n", amountPaid);
double overpay = amountPaid - Months(principal, annualInterestRate, monthlyPayment)*monthlyPayment;
printf("You overpaid:\t\t\t\t $%.2f\n", overpay);
int extraMonthlyPayment = monthlyPayment*.1;
int monthsEarly = ((log((monthlyPayment*1.1))-log((monthlyPayment*1.1)-annualInterestRate/1200.0*principal))/log(annualInterestRate/1200.0+1.0));
int newMonths = Months(principal, annualInterestRate, monthlyPayment)-monthsEarly;
printf("Did you know if you paid an additional $%d per month, you would pay off your loan %d months earlier?\n\n", extraMonthlyPayment, newMonths);
printf("Do you wish to calculate another payoff? (Y/N):\t");
scanf("%c", &response);
}
while(response == 'Y' || response == 'y');
printf("Thank you for using the Payment Calculator!\n\n");
return(0);
}
Using scanf is suboptimal.
I'd add a
while (getchar());
line before the last scanf.
Please make sure you don't miss or mangle other data due to the newlines in the input buffer.
I have a price for a car, let's say 10000. I want to apply a 20% sale to this price.
I have a struct in which auta->cena is a float.
int year, i, n=0, pocet=1, sale;
scanf(" %d", &year);
scanf(" %d", &sale);
for(i=1; i<=pocet; i++){
if(year == auta->rok){
++n;
pocet++;
auta->cena *= ((float)(100 - sale) / 100); //calculate price after 20% sale
//temp = ((int)(temp * 100 + 0.5)) / 100.0; //use this formula to round up final price, it doesnt work, I get 0.00
printf("%.2f\n", auta->cena);
}
auta = auta->dalsi;
}
I am not good at converting—can anyone explain it to me, please? How should I go about it?
If you're going to print the value with %.2f, you don't have to do any rounding. But if you want to round the value internally, the following will work. I'm printing it with %.5f to show that the value is really changed.
#include <stdio.h>
int main() {
int discount;
double price;
printf("Enter a price: ");
scanf(" %lf", &price);
printf("Enter a percentage discount: ");
scanf(" %d", &discount);
price *= (100.0 - discount) / 100; // calculate price after discount
printf("after discount: %.5f\n", price);
price = (int)(100 * price + 0.5) / 100.0;
printf("rounded: %.5f\n", price);
}
I'm using double above to preserve enough precision to demonstrate that the calculation works with, for example, a price of 10000 and a discount of 20. If you do it with float, you lose enough precision that there's no point in rounding to the nearest cent. The internal value will be imprecise anyway.
Here is a variant of the same code using float:
#include <stdio.h>
int main() {
int discount;
float price;
printf("Enter a price: ");
scanf(" %f", &price);
printf("Enter a percentage discount: ");
scanf(" %d", &discount);
price *= (100.0 - discount) / 100; // calculate price after discount
printf("after discount: %.5f\n", price);
price = (int)(100 * price + 0.5) / 100.0;
printf("rounded up: %.5f\n", price);
return 0;
}
Here is my code:
#include <stdio.h>
float calculateBalance(float payment, float balance, float rate);
int main(void){
float loanAmount, interestRate, monthlyPayment;
printf("Enter amount of loan:");
scanf("%.2f", &loanAmount);
printf("\nEnter interest rate:");
scanf("%.1f", &interestRate);
printf("\nEnter monthly payment:");
scanf("%.2f", &monthlyPayment);
loanAmount = calculateBalance(monthlyPayment, loanAmount, interestRate);
printf("Balance remaining after first payment: %.2f\n", loanAmount);
loanAmount = calculateBalance(monthlyPayment, loanAmount, interestRate);
printf("Balance remaining after second payment: %.2f\n", loanAmount);
loanAmount = calculateBalance(monthlyPayment, loanAmount, interestRate);
printf("Balance remaining after third payment: %.2f\n", loanAmount);
}
float calculateBalance(float payment, float balance, float rate){
return (balance-payment + balance*rate);
}
I am trying to calculate the balance after three payments are made; However, when I try and run the code in terminal, it's not letting me input anything, and instead is just reading in random numbers and then calculating the balance after three payments(I want to be able to input things, but it isn't allowing me to). How do I fix this?
The issue is in the fmt string itself. As #T.C. said in a comment
try using
scanf("%f", &var);
instead. If there are only 2 dec values, then it will only read that.
You can try a adding a getc(stdin); after each scanf.
Are there any C language programmers on here that can help me figure this out?
I am having trouble getting the calculation for the average miles per gallon to work and my head is spinning. I would really appreciate if anyone have a solution ^_^
int x, number_of_tanks = 3;
double total_num1, total_num2;
double total_miles_per_gallon;
float division, avg;
float num1, num2;
for (x = 1; x <= 3; x++)
{
printf("Enter the number of gallons used for tank #%i: ",x);
scanf("%f", &num1);
fflush(stdin); /* clear input buffer */
printf("Enter the number of miles driven: ");
scanf("%f", &num2);
fflush(stdin); /* clear input buffer */
/*--------------------------------------------------------------*/
/* calculate and output the miles per gallon from user input. */
/* ------------------------------------------------------------ */
division = num2 / (float) num1;
printf("The miles per gallon for this tank %.1f divided by %.1f is %.1f", \
num2, num1, division);
total_num2 = total_num2 + num2;
printf("The total of miles is %f\n", total_num2);
total_num1 = total_num1 + num1;
printf("The total of gallons is %f\n", total_num1);
}
avg = (double) total_num2 / total_num1;
printf("Overall average miles per gallon for three tanks: %.1f", avg);
You don't initialise your totals, so they are undefined. When you start adding to them, you get undefined results. I bet that's what you mean by it not working.
Do this:
double total_num1 = 0;
double total_num2 = 0;