assignment at school asks me to find the present value using double, and void. i was able to write my code up to a certain degree but the result is not what i was expecting.. i ended up separating the present value into different section so at the end i'd multiply the amount given with the rest.. any tips on how to make the code actually work the way its supposed to?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double payment,year_term, interest;
double sum;
double Power;
double presentv;
double present;
cout << "Hello, how are you doing?" << endl;
cout << "Please insert a payment amount" << endl;
cin >> payment;
cout << " amount inserted: " << payment << endl;
cout << "Enter number of years" << endl;
cin >> year_term;
cout << " number of years: " << year_term << endl;
cout << "Enter interest rate" << endl;
cin >> interest;
cout << " the interest is: " << interest << "%" << endl;
Presentv = ((1 - (pow((1 + interest),year_term))))/interest;
cout << " the value: " << Presentv << endl;
presentva = payment * Presentv;
cout << " the present value is: " << presentva << endl;
}
I've been teaching myself C++ on and off for a few months and now I'm trying to make a payroll system. Here's my code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void wageCompute (int, int);
int main()
{
int loopTimes=0;
int empNum=100, workHours, otHours, rate, bPay, otPay, grossPay;
string empName, empPos;
cout << "PAYROLL FOR THE MONTH OF MARCH" << endl;
cout << "Employees: " << empNum << endl;
while (empNum>loopTimes)
{
cout << "NAME: ";
cin.ignore();
getline (cin, empName);
cout << "\nPOSITION: ";
getline (cin, empPos);
cout << "\nHOURS WORKED: ";
cin >> workHours;
cout << "\nWAGE PER HOUR: ";
cin >> rate;
bPay = workHours*rate;
cout << "YOUR BASE PAY IS: " << bPay << endl << endl;
cout << "HOURS WORKED OVERTIME: ";
cin >> otHours;
otPay = (1.5*rate)*otHours;
cout << "\nOVERTIME PAY: " << otPay << endl;
grossPay = bPay + otPay;
cout << "GROSS PAY: " << grossPay << endl;
wageCompute(bPay, grossPay);
loopTimes++;
}
return EXIT_SUCCESS;
}
void wageCompute(int bPay, int grossPay)
{
double rate, dedInsurance, dedMedical, totDeduct, netPay, tax;
if (bPay<10001)
{
rate = 0.05;
}
else if (bPay<15001)
{
rate = 0.1;
}
else if (bPay<20001)
{
rate = 0.15;
}
else
{
rate = .2;
}
tax = bPay*rate;
dedInsurance = bPay*0.05;
dedMedical = bPay*0.01;
totDeduct = tax + dedInsurance + dedMedical;
cout << "TAX: " << tax << endl;
cout << "SSS DEDUCTION: " << dedInsurance << endl;
cout << "Medicare DEDUCTION: " << dedMedical << endl;
cout << "TOTAL DEDUCTIONS: " << totDeduct << endl;
netPay = grossPay - totDeduct;
cout << "NET PAY: " << netPay << endl;
}
The part where it goes wrong is when I input a certain value for the Hours Worked, Wage per Hour and Hours worked overtime. The program checks the basic pay for the suitable amount of tax it should deduct, what I input was 160 for the hours worked, 100 for the wage per hour, and 10 for overtime. I've tried lessening and increasing it and it worked just fine it seems that it's just these combination of numbers is the part where it goes wrong.
A screenshot of the output:
Your question isn't very clear but I suspect that what you are seeing here is a well known limitation of floating point numbers; numbers that are easy to represent exactly in base 10 don't have an exact representation in base 2. One example : 0.1 in base 10 is 0.0001100110011… repeating in base 2; the accuracy of the approximation depends on how many bits one is willing to use to write it.
An alternative approach is to use integer arithmetic with a known precision, so say you want to calculate to the nearest hundredth of a penny (I'm using UK currency here). Represent £1.01 as 10100 and when your finished val / 10000 is the pounds and (val % 10000) / 100 is the pence. If needed you can implement some more complex rules around rounding for the pence.
I'm writing a program which will calculate monthly payments for a loan. It is not giving the correct answer though. Here is my code:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt;
int NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = YearlyInt / 12;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: " << Payment << endl;
AmountPaid = Payment * 36;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
}
The program uses this formula:
MonthlyInt * pow(1 + MonthlyInt, NumPayments) * LoanAmount
Payment = ---------------------------------------------------------------
pow(1 + MonthlyInt, NumPayments) - 1
This is what I get as an output:
Enter the loan amount (LoanAmount) --> 10000
Enter the YEARLY interest rate as a percentage --> 12
Enter number of payments --> 36
Loan amount: 10000
Yearly Interest Rate: 12%
Number of Payments: 36
Monthly Payment: 10000
Amount Paid Back: 360000
Interest Paid: 350000
Program Over
Press Enter to end -->
Press any key to continue . . .
As you can see, the Loan amount is clearly wrong. How can I fix my code?
Step 1: MonthlyInt does NOT equal YearlyInt / 12 because of the effect of compounding interest. The general formula for converting between rate of a smaller period and the equivalent rate of a larger period is: (1 + r) ^n = 1 + R. So in this case r = MonthlyInt and R = YearlyInt. Therefore, the first order of business is to change
from:
MonthlyInt = YearlyInt / 12;
to:
MonthlyInt = pow ( (1.0 + YearlyInt) , (1.0/NumPayments) ) - 1.0; // note decimals!
Step 2: Add a line that prints MonthlyInt so that you can verify the calculation. :)
Step 3: Change AmountPaid = Payment * 36; to AmountPaid = Payment * NumPayments;
Step 4: Optionally, add dollar signs and clean up the decimals.
We must add the header #include<iomanip>, then set the number of decimals with cout << setprecision(n) << fixed << whateverVariable, where n equals the number of decimal places you want.
Revised code:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double YearlyInt = -1, LoanAmount = -1, Payment = -1, AmountPaid = -1, MonthlyInt = -1;
int NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a decimal number (e.g. 3.25% as .0325) --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: $" << setprecision(2) << fixed << LoanAmount << endl;
cout << "Yearly Interest Rate: " << setprecision(3) << YearlyInt * 100 << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = pow ( (1.0 + YearlyInt) , (1.0/NumPayments) ) - 1.0;
cout << "MonthlyInt: " << MonthlyInt*100 << "%" << endl;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: $" << setprecision(2) << Payment << endl;
AmountPaid = Payment * NumPayments;
cout << "Amount Paid Back: $" << AmountPaid << endl;
cout << "Interest Paid: $" << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
}
Assumptions: the loan has a no-fees APR of YearlyInt to be compounded monthly, with monthly payments, with the first payment applied on the last day of the same month in which the loan is originated, and with all “on time” payments (whatever that is defined as, by the lendor) being applied as if paid on the last day of the applicable period.
There are a few problems:
You enter rate in percents, so convert them to a decimal number: MonthlyInt/100.0
Your number of payments should be either fixed, or entered by user. Now it is firstly read in, but then there is 36 used in the code. It should be replaced with the proper variable.
Be careful with integer division. There is no mistake at the moment, but to avoid such, use 1.0 and 100.0 instead of just 1 and 100 if you want to be sure you have floats.
Be sure your math is right. In fact, this should be the very first thing you do. This is a programming site though, so it's off-topic here.
(optional) Conventionally, variable names shouldn't start with capital letter.
Here is a program which correctly calculates the payments assuming the following:
Yearly interest is calculated as compounded monthly interest.
There are no fees applied to the loan.
The repayments start one month after the loan is given.
The monthly payment amounts do not change.
No monthly payments are missed.
.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt;
int NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of monthly payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Monthly Payments: " << NumPayments << endl;
MonthlyInt = pow( 1 + YearlyInt/100, 1.0/12 );
Payment = LoanAmount * pow( MonthlyInt, NumPayments ) *
( MonthlyInt - 1 ) /
( pow( MonthlyInt, NumPayments ) - 1 );
cout << "Monthly Payment: " << Payment << endl;
AmountPaid = Payment * NumPayments;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
return 0;
}
Is the program supposed to calculate compound interest or simple interest?
Your calculation appears to be incorrect. You calculate a monthly interest rate, which makes it look like simple interest, but you use pow, which indicates something to do with compound interest. You should probably look into that.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt, NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = (YearlyInt/100.0) / 12;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: " << Payment << endl;
AmountPaid = Payment * 36;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
Got it simply switched MonthlyInt = YearlyInt / 12; to MonthlyInt = (YearlyInt/100.0) / 12;
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt, NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = (YearlyInt/100.0) / 12;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: " << Payment << endl;
//correction to Amount paid Value
AmountPaid = Payment * NumPayments;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
}
In case you don't want to use pow functions, you can directly calculate the geometric sum and nth term of the series yourself (the pow() comes from geometric terms and sum). See the proof here: https://mortgagecalculator.mes.fm/amortization-formula-proof
int months = 60;
float principle = 10000;
float rate = 6.5f/100;
float monthly_factor = 1 + rate / 12;
float temp = 1, temp2= monthly_factor;
for(int i = 0;i < months - 1; i++){ //this for loop calculates the geometric sum with ratio of monthly_factor in temp and pow(monthly_factor,n) in temp2
temp *= monthly_factor;
temp2 *= monthly_factor;
temp += 1;
}
float monthly_payment = principle * temp2 / temp;
I want make three items to calculate then convert
Example:
item1 + item2 + item3 = `total`
Then, convert total to Saudi riyals
// sss.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
using namespace std;
int main()
{
cout << "conver from US dollar to Saudi ryals" << endl;
double dollars1;
double dollars2;
double dollars3;
double ryals = 3.75;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars1;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars2;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars3;
cout << "US $" << dollars1 + dollars2 + dollars3 << " equals " << ryals * (dollars1 + dollars2 + dollars3) << " Saudi ryals." << endl;
cout << "that's it" << endl;
cin.get();
cin.get();
}
Today's exchange rate for US dollar to Saudi riyal is $1 US to 3.75 riyal.
Your logic seems correct here : ryals * ( dollars1 + dollars2 + dollars2) but you for starters have two mains. This cannot happen in any circumstance. Also I'd re-think your naming convention of dollars1, dollars2, dollars3
Your #include <stdafx> was auto generated by Visual Studio, which is why I'm assuming two mains were created for you. See here for more information.
Try this:
#include "iostream"
using namespace std;
int main()
{
cout << "conver from US dollar to Saudi ryals" << endl;
double dollars1;
double dollars2;
double dollars3;
double ryals = 3.75;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars1;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars2;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars3;
cout << "US $" << dollars1 + dollars2 + dollars3 << " equals " << ryals * (dollars1 + dollars2 + dollars3) << " Saudi ryals." << endl;
cout << "that's it" << endl;
cin.get();
}
My program I have been working on is supposed to output the following:
* The number of gallons of paint required
* The hours of labor required
* The cost of the paint
* The labor charges
* The total cost of the paint job
However, it displays 0 in every field.. What have I done wrong now?
Your help would be greatly appreciated.
Here is my code:
//Headers
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
void PaintJobEstimator(double gallonprice, double calc)
{
float numBucket=0;
float hours=0;
float bucketCost=0;
float laborCharges=0;
float totalCost=0;
//calculates number of buckets of paint (gallons) needed
numBucket=numBucket+calc*(1/115);
//calculates paint cost
bucketCost=bucketCost+gallonprice*numBucket;
//calculates labor hour
hours=hours+calc*(8/115);
//calculates labor charges
laborCharges=hours*18;
//calculates total cost
totalCost=totalCost+bucketCost+laborCharges;
//Console output
cout << "The number of Gallons of paint required:\t" << setprecision(2) << numBucket << endl;
cout << "The hours of labor required:\t" << setprecision(2) << hours << " hrs" << endl;
cout << "The labor charges:\t$" << setprecision(2) << laborCharges << endl;
cout << "The cost of the paint:\t$" << setprecision(2) << bucketCost << endl;
cout << "The total cost of the paint job:\t$" << setprecision(2) << totalCost << endl;
}
void main ()
{
int rooms;
double calc=0;
double wallspace;
double gallonprice;
cout << "=========================================================\n";
cout << "___________________Paint Job Estimator___________________\n";
cout << "_________________________________________________________\n";
cout << endl;
cout << "Enter the number of rooms: ";
cin >> rooms;
while (rooms<1) //validates rooms
{
cout << "Invalid entry, enter one or more rooms:\t";
cin >> rooms;
}
for (int roomNum=1;
roomNum<=rooms;
roomNum++)
{
cout << "Enter the wall space in square meters for room " << roomNum << ":\t" << endl;
cin >> wallspace;
while (wallspace < 0.01)//validates wallspace
{
cout << "Invalid entry, please re-enter the wall area for room " << roomNum << ":\t";
cin >> wallspace;
}
calc=calc+wallspace;
}//end loop
cout << "\nEnter price of the paint per gallon: ";
cin >> gallonprice;
if (gallonprice <10) //validates price per gallon
{
cout << "Invalid entry, Reenter price at a $10.00 minimum: ";
cin >> gallonprice;
}
PaintJobEstimator(gallonprice,wallspace);
system ("pause");
}
Here is a screenshot of the console:
You're multiplying by zero in some of the calculations. For example, in the following line of code:
numBucket=numBucket+calc*(1/115);
You put 1/115 in parenthesis, which evaluates to zero because of integer division. To achieve the desired effect, try:
numBucket = calc / 115.0f;