4 Replies - 326 Views - Last Post: 12 February 2014 - 03:00 AM Rate Topic: -----

#1 alina_the_coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-February 14

Interest Calculator math part

Posted 11 February 2014 - 11:42 PM

I need help with the math part.

The equation I need to use is:
payment = [(rate * (1 + rate)^number payments ) / ((1 + rate)^number of payments -1)] * loan

Also, the value of rate in the above equation is (interest/12)/100. So 12% annual interest would be 1% monthly interest.

Would anyone please steer me in the right direction!
All help is greatly appreciated!

heres my code:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int main(){
	//Declaration of variables as integers and doubles.
	int option, loan, payamount, convertedrate, interestrate, days,speed, hours, distance;
	double interest, numberpayments, initialdollars;
	
	//Display the menu options.
	cout<<"1) Interest"<<endl;
	cout<<"2) Compound"<<endl;
	cout<<"3) Distance"<<endl;
	cout<<"4) Quit"<<endl;
	cout<<"\n";
	cout<<"Choose an option: ";
	cin>>option;
	
	//Result of the users input:
	switch (option){
		//When the user enters a 1,program prompts them to input information
		//to calculate interest.
		case 1:
			//Program asks user for a loan amount.
			cout<<"\n";
			cout<<"Enter the amount of the loan: $";
			cin>>loan;
			cout<<"\n";
			//If the input is not in the range of 100-20,000, the program will
			//continue asking until the user inputs a number of the correct range.
			if (loan < 100 || loan > 20000)
				do{
					cout<<"Please enter a loan amount between $100 and $20,000";
					cout<<"\n"<<endl;
					cout<<"Enter the amount of the loan: $";
					cin>>loan;
					cout<<"\n";
				}while (loan < 100 || loan > 20000);
			
			//Program asks user for an annual interest rate.
			cout<<"Enter the Annual Interest Rate: $";
			cin>>interest;	
			cout<<"\n";
			//If the input is not in the range of 1-20, the program will
			//continue asking until the user inputs a number of the correct range.
			if (interest < 1 || interest > 20)
				do{
					cout<<"Please enter an Annual Interest Rate between 1 and 20";
					cout<<"\n"<<endl;
					cout<<"Enter the Annual Interest Rate: $";
					cin>>interest;
					cout<<"\n";
				}while (interest < 1 || interest > 20);
				
			//Program asks user for a number of payments.
			cout<<"Enter the number of payments: ";
			cin>>numberpayments;
			cout<<"\n";	
			//If the input is not in the range of 100-20,000, the program will
			//continue asking until the user inputs a number of the correct range.
			if (numberpayments < 5 || numberpayments > 360)
				do{
					cout<<"Please enter a number of payments between 5 and 360 months";
					cout<<"\n"<<endl;
					cout<<"Enter the number of payments: ";
					cin>>numberpayments;
					cout<<"\n";
				}while (numberpayments < 5 || numberpayments > 360);
			//Calculation for the loan.
			
				
			//NEED HELP WITH FORMULA******************
//			convertedrate=(interest/12)/100
//			payamount= ((convertedrate * pow((1 + convertedrate), numberpayments))/  (pow((1 + convertedrate), numberpayments) -1))*loan;
			//Displays the user input and calculation for inputs in a readable format.
//			printf("")

		//When the user enters a 2, program prompts them to input information
		//to calculate coumpound.
		case 2:
			//Program asks user for the amount in dollars.
			cout<<"Initial amount in dollars?";
			cin>>initialdollars;
			//If the input is not in the range of 10-10,000, the program will
			//continue asking until the user inputs a number of the correct range.
			if (initialdollars < 10 || initialdollars > 10000)
				do{
					cout<<"\n";
					cout<<"Dollar amount should be between 10 and 10000."<<endl;
					cout<<"Try again!";
					cin>>initialdollars;
					cout<<"\n";
				}while (initialdollars < 10 || initialdollars > 10000);
			cout<<"\n";
			
			//Program asks user for an interest rate in percentage
			cout<<"Interest rate in percentage?";
			cin>>interestrate;
			//If the input is not in the range of 1-22, the program will
			//continue asking until the user inputs a number of the correct range.
			if (interestrate < 1 || interestrate > 22)
				do{
					cout<<"\n";
					cout<<"Interest rate be between 1 and 22."<<endl;
					cout<<"Try again!";
					cin>>interestrate;
					cout<<"\n";
				}while (interestrate < 1 || interestrate > 22);
			cout<<"\n";
			
			//Program asks user for a number of days.
			cout<<"Number of days?";
			cin>>days;
			//If the input is not in the range of 2-30, the program will
			//continue asking until the user inputs a number of the correct range.
			if (days < 2 || days > 30)
				do{
					cout<<"\n";
					cout<<"Interest rate be between 2 and 30."<<endl;
					cout<<"Try again!";
					cin>>days;
					cout<<"\n";
				}while (days < 2 || days > 30);
			cout<<"\n";
			//Display of user input and calculation of inputs in a readable format.
			//LOOP FOR COMPOUND GOES HERE
			//NEED HELP
			
		//When the user enters a 3,program prompts them to input information
		//to calculate speed.					
		case 3:
			//Program asks user for the speed.
			cout<<"What is the speed of the vehicle in mph? ";
			cin>>speed;
			//If the input is not in the range of 20-200, the program will
			//continue asking until the user inputs a number of the correct range.
			if (speed < 20 || speed > 200)
				do{
					cout<<"\n";
					cout<<"Wrong range. Try again ";
					cin>>speed;
				}while (speed < 20 || speed > 200);
			cout<<"\n";
			
			//Program asks user for the number of hours.
			cout<<"How many hours has it traveled? ";
			cin>>hours;
			//If the input is less than 3, the program will
			//continue asking until the user inputs a greater than or equal to 3.
			if (hours < 3)
				do{
					cout<<"\n";
					cout<<"Come on... A real number this time. Try again. ";
					cin>>hours;
				}while (hours < 3);
			cout<<"\n";
			
			distance=speed * hours;
			//LOOP TO OUTPUT THE INFO
			//NEED HELP	
			
		//When the user enters a 4,program closes.
		case 4:
			cout<<"goodbye";
			return 0;	
		default:
			cout<<"wrong entry: try again"<<endl;
			cout<<"\n";
			cout<<"1) Interest"<<endl;
			cout<<"2) Compound"<<endl;
			cout<<"3) Distance"<<endl;
			cout<<"4) QUIT"<<endl;
			cout<<"\n";
			cout<<"Choose an option: ";
			cin>>option;	
				
			
	}
system("pause>nul");//does this pause the screen?		
//return 0; // check if you need to pause the screen in specs!	
}


This post has been edited by Skydiver: 12 February 2014 - 12:33 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: Interest Calculator math part

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: Interest Calculator math part

Posted 12 February 2014 - 12:46 AM

The initial problem is that you've forgotten the difference between integer division and floating point division. In C/C++ these two snippets of code will result in two different things:
float x = 3 / 4;            // x will be 0.
float y = 3 / (float) 4;    // y will be approximately 0.75



There are other issues with your code, but this should hopefully get you unstuck.

In the future, it is much better to put your question outside of your code. Very few the regulars here will pore over code, particularly large amounts of code, without a guiding question or description of what the problem is.
Was This Post Helpful? 0
  • +
  • -

#3 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 244
  • View blog
  • Posts: 884
  • Joined: 18-April 09

Re: Interest Calculator math part

Posted 12 February 2014 - 01:41 AM

Hi,

Its nice to simplify your main() function and make it as simple and concise as possible. You could do this by creating functions. The payment equation could also be implemented in a separate function. Functions are your friend.
Was This Post Helpful? 0
  • +
  • -

#4 alina_the_coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-February 14

Re: Interest Calculator math part

Posted 12 February 2014 - 01:46 AM

View PostSkydiver, on 12 February 2014 - 12:46 AM, said:

The initial problem is that you've forgotten the difference between integer division and floating point division. In C/C++ these two snippets of code will result in two different things:
float x = 3 / 4;            // x will be 0.
float y = 3 / (float) 4;    // y will be approximately 0.75



There are other issues with your code, but this should hopefully get you unstuck.

In the future, it is much better to put your question outside of your code. Very few the regulars here will pore over code, particularly large amounts of code, without a guiding question or description of what the problem is.


I'll keep trying.

And sorry about that, I'm new here and still figuring things out.

View Posttarmizi_adam2005, on 12 February 2014 - 01:41 AM, said:

Hi,

Its nice to simplify your main() function and make it as simple and concise as possible. You could do this by creating functions. The payment equation could also be implemented in a separate function. Functions are your friend.


I'm not allowed to use functions, my class hasn't gotten to that chapter yet. But I remember functions from Visual Basic, and I agree with you, they are my friend!
Was This Post Helpful? 0
  • +
  • -

#5 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 244
  • View blog
  • Posts: 884
  • Joined: 18-April 09

Re: Interest Calculator math part

Posted 12 February 2014 - 03:00 AM

I would recommend you do the calculations on paper first then implement it in code. That way, you can check if your implementation in code gives the right answer or otherwise.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1