I am trying to create a C program which calculates the monthly payment and print a table of payment schedule for a fixed rate loan. My issue is that the loop only iterates once but I cannot figure out why. Here is how the output should looked like:
#include <stdio.h>
#include <math.h>
double calculatePayments(double loan, double rate, int payment);
int main(){
double loan,rate,monthly,principal,interest,balance;
int payment, counter;
counter = 0;
balance = 0.0;
printf("Enter amount of loan: ");
scanf("%lf", &loan);
printf("Enter interest rate per year:%% ");
scanf("%lf", &rate);
printf("Enter number of payments: ");
scanf("%d", &payment);
monthly = calculatePayments(loan, rate, payment);
printf("Monthly payment should be %.2f\n", monthly);
printf("—————AMORTIZATION SCHEDULE—————\n ");
printf("N\tPayment\tPrincipal\tInterest\tBalance\n ");
do{
rate = rate/12/100;
interest = loan * rate;
principal = monthly - interest;
balance = loan - principal;
counter++;
printf("%d\t%.2f\t%.2f\t\t%.2f\t\t%.2f\n", counter, monthly, principal, interest, balance);
}while(balance < 0);
return 0;
}
double calculatePayments(double loan, double rate, int payment) {
rate = rate/12/100;
double mul = pow(1+rate, payment);
return (loan * mul * rate) / (mul - 1);
}
Changing up the do ... while loop
// e.g. 7.5/12/100 = 0.00625
rate = rate/12/100;
do{
// e.g. 500*0.00625=3.125
interest = loan * rate;
// e.g. 101.88-3.125=98.76
principal = monthly - interest;
// e.g. 500-98.76=401.24
balance = loan - principal;
// update loan
loan = balance;
counter++;
printf("%d\t%.2f\t%.2f\t\t%.2f\t\t%.2f\n", counter, monthly, principal, interest, balance);
}while(balance > principal);
Balance would never be < 0. That is why you loop is only running once. If you had done a while loop instead of do while it wouldn't run at all.
Change balance < 0 to balance > 0
You are checking to loop only if balance is less than Zero.
but at the first iteration the value of balance would be 401$ (not < 0)
So breaking.
You should be checking for balance > 0 in while
Related
I need to figure out how to continuously add to the running total of avg mpg and divide by how many times it goes through.
#include <stdio.h>
int main() {
int miles;
int gallons;
int mpg;
int avg;
while (miles != -1) {
printf("Enter number of miles driven(-1 to quit): ");
scanf("%d", &miles);
printf("Enter gallons used: ");
scanf("%d", &gallons);
mpg = miles / gallons;
avg = mpg;
printf("MPG this trip: %d\n", mpg);
printf("Avg MPG(so far): %d\n", avg);
}
}
Okay so I think I got what you are trying to say, so first of all you should initialize avg = 0; and instead of changing avg = mpg; every time the loop runs you should do avg += mpg; so that it will add the previous values to next time the loop runs, Take another int to find the total average, initialize from 0 int t_avg = 0; and for next thing, dividing how many times it goes through you should take a variable and initialize it t = 0; and just increment t every time the loop runs so you will get the time loop goes on and you just have to divide it with avg.
And I would suggest you to use do while loop instead of while so that would be much better.
Hope this is what you were looking for.
#include <stdio.h>
int main() {
int miles;
int gallons;
int mpg;
int avg = 0, t = 0,t_avg = 0;
do {
t++;
printf("Enter number of miles driven(-1 to quit): ");
scanf("%d", &miles);
printf("Enter gallons used: ");
scanf("%d", &gallons);
mpg = miles / gallons;
avg += mpg;
t_avg = avg/t;
// adding average every time and diving every time the loop runs
printf("MPG this trip: %d\n", mpg);
printf("Avg MPG(so far): %d\n", t_avg);
}while(miles != -1);
}
here is the picture of result of this program/My question is about how to keep the running total in loop?
because in my program, the total debt would only based on the last input of the user before the loop gets terminated instead getting the total debt from all creditors
/This program is for calculating the total debt
#include <stdio.h>
#include <string.h>
int main()
{
float cb,ir,si,totaldebt; //local declaration
int time,i,sum=0;
char c[25];
printf("------------Welcome to Debt Management System-------------");
printf("\nNOTE:\nDear users,\n\tYou have to enter the creditor's name if you have a debt. But if you have nothing debt left, just enter none");
for (i=1;i>=1;i++)
{
printf("\n%d)Name of the creditor: ",i);
//the user inputs the name of the creditor
scanf("%s", c);
if (strcmp(c, "none") == 0) // condition wherein if the user inputs "none" in the name of the creditor, the loop will terminate
{
break;
}
printf("Enter your current balance: ");//the user inputs current balance from the said creditor
scanf("%f",&cb);
printf("Enter its interest rate: "); //the user inputs the interest rate of of the debt
scanf("%f",&ir);
printf("Enter time for the loan: ");//the user inputs month
scanf("%d",&time);
si=cb*ir*time/100;//simple interest
totaldebt=si+cb; //simple interest + current balance
sum+=totaldebt;
}
printf("The total balance you have for now is: %.2f\n",totaldebt); //
if (totaldebt<=5000)
{
printf("\nCongratulations!\n You only have %.2f debt left\n",totaldebt);
}
else
{
printf("\nWork harder because out of DEBT is out of DANGER\n");
}
return 0;
}
You're already keeping track of the running total in sum. You just need to use that:
float sum = 0;
...
printf("The total balance you have for now is: %.2f\n",sum ); //
if (sum <=5000)
{
printf("\nCongratulations!\n You only have %.2f debt left\n",sum );
}
else
{
printf("\nWork harder because out of DEBT is out of DANGER\n");
}
Good day! In a program I am writing for school we must make a cash register type program, seems simple enough, but for the life of me I can not get it to work. After taking in the number of products bought, then the price of all, the program must ask for the cash to pay, then give back the change. BUT the change must be given in amount of loonies back (or $1 bills), and then just the remaining cents. Help? I've gotten the loonies to work (somewhat) but I don't know how to do the change back.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int itemNum, justChange;
double prodPrice, tax, cashGiven, change, purchasePrice, changeGiven, changeBack, cashBack;
float totalPrice;
//Num of items
printf ("Number of items: ");
scanf("%d", &itemNum);
//Price of items
printf("Please enter price of items: ");
scanf("%lf", &prodPrice);
//Math Stuff
purchasePrice = itemNum*prodPrice;
tax = purchasePrice * 0.13;
totalPrice = purchasePrice*1.13;
//find change alone
//justChange = totalPrice
//Price Output
printf("Purchase price is: %.2lf \n",purchasePrice );
printf("TAX (HST 13%): %.2lf\n",tax );
printf("Total price is: %.2lf \n",totalPrice );
printf("Please Enter Cash: ");
scanf("%lf", &cashGiven);
printf("Please Enter Change: ");
scanf("%lf", &changeGiven);
//MAth stuuff again
double endCash;
double loony;
int yoloswag;
endCash = cashGiven - totalPrice;
loony = endCash/1;
loony = loony--;
if (loony<0)
printf ("Loonies: 0");
else
printf("Loonies: %.0lf \n",loony );
printf("change: %d ", totalPrice-floor(totalPrice) );
return 0;
}
Create an array with possible change values;
double cashValues[6] = {1, 0.5, 0.2, 0.1, 0.05, 0.01};
Then create a for loop in which you try to subtract the possible change values from the difference until the difference is zero.
double difference;
difference = cashGiven - totalPrice;
while (difference != 0) {
for(int i=0; i<6; i++) {
if(cashValues[i] <= difference) {
difference -= cashValues[i];
printf("%f \n", cashValues[i]);
i=0;
}
}
}
Im trying to write this code for school, and im absoultly stuck on what im doing wrong, if anyone could just point me in the right direction, that would be helpful. Trying to learn as much as i can.
My program doesnt calculate out, how much is owed at the end of each month, after subtrackting the payment, and then adding the interest.
IT just displays the same value.
#include<stdio.h>
int main()
{
float loan;
float interest;
int n;
float outstanding;
float outstanding2;
float princeable;
float payment;
printf("\nEnter the amount of the loan: \n ");
scanf("%f" , &loan);
printf("\nEnter monthly interest percentage\n ");
scanf("%f" , &interest);
printf("\nEnter monthly payments: \n ");
scanf("%f" , &payment);
printf("\nEnter number of monthly Payments: \n ");
scanf("%i" , &n);
while (n >= 0) {
outstanding = (loan - payment);
outstanding = (outstanding * (1 + (interest/100)));
printf("\Outstanding Balance after %i =%.2f\n\n", n, outstanding);
n--;
}
return 0;
}
In each iteration, you should calculate outstanding based on its previous value, and not on the initial loan's value, because you also pay interest for interest.
outstanding = loan;
while (n > 0) {
outstanding = (outstanding - payment);
outstanding = (outstanding * (1 + (interest/100)));
printf("\Outstanding Balance after %i =%.2f\n\n", n, outstanding);
n--;
}
This line:
outstanding = (loan - payment);
within the loop is incorrect since it starts with the initial loan value each time. It should be:
outstanding = (outstanding - payment);
You'll also have to set outstanding to loan before entering the loop.
On top of that, you have one too many loop iterations, and an illegal escape sequence \O in your printf string.
/*Taking input from user to calculate the cost of a travel plan
using the number of travelers/ number of stops/ cost of each stop
giving both the half cost of a trip and its full price*/
int calculate( int total);
#include <stdio.h>
int main(void)
{
float customer, stops, response, cost, i, stop, total, halfcost, totaltrip;
i=0;
printf("Welcome to Airline express.\n");
printf("Would you like to calculate the cost of a trip?");
scanf("%f", &response);
while ( response != 0 )
{
printf("Please enter how many customer will be traveling. Children under 12 are counted as .5:");
scanf("%f", &customer);
if (customer < 0)
customer = 3;
printf("Please enter how many stops there are:");
scanf("%f", &stop);
while (i<stop)
{
printf("Please enter cost of the next stop:");
scanf("%f", &cost);
if (cost < 0)
cost = 100;
total +=cost;
i++;
}
halfcost = calculate(total);
printf("Half cost is: %f", halfcost);
totaltrip = total * customer;
printf("Total cost for trip is: %f\n", totaltrip);
printf("Would you like to calculate another trip?");
scanf("%f", &response);
}
return 0;
}
int calculate( int total)
{
return total/2;
}
I'm having issues with the inputs, I'm trying to make the loop run as the user request another calculations. But when ever it re runs another test it keeps the input of the previous test is there a way to reset the variables to 0?
Ive tried assign all of the variable inside and outside of the loop but I'm kinda lost on what to go from here.
Move the declaration of the variables to sit inside the loop.
while ( response != 0 )
{
float customer = 0, stops = 0, response = 0, cost = 0, i = 0, stop = 0, total = 0, halfcost = 0, totaltrip = 0;
...
}