Welcome to Dream.In.Code
Become a Java Expert!

Join 149,609 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,872 people online right now. Registration is fast and FREE... Join Now!




Calculating Interest based on user inputed information

 
Reply to this topicStart new topic

Calculating Interest based on user inputed information

m_milam
16 Sep, 2007 - 07:39 PM
Post #1

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 23


My Contributions
CODE

import java.util.Scanner;

public class Exercise2_13 {
    /**Main method */
    
    public static void main(String[] args) {
        
        //create a scanner for input
        Scanner input = new Scanner(System.in);
        
         //Enter yearly balance
        System.out.print("Enter Balance: ");
        double Balance = input.nextDouble();
        
        //Enter yearly interest rate
        System.out.print("Enter Annual Interest Rate: ");
        double Interest = input.nextDouble();
        
        //Display results
        System.out.print("The Interest is " + Interest);
        
        System.exit(0);
  }
}


I have this working halfway. I am trying to write a program that will display the interest based on the user inputing (a) their balance and (cool.gif their annual interest rate. I have it working up to the point of computing the interest. It prompts the user for the balance and then the interest rate. Then it display the message " The interest is --" . The number it gives for the interest is the number that was inputed as the interest rate.

The formula I have for the interest rate is: interest = balance x (annualInterestRate /1200). How do I write this into the code so that it does the equation? I have tried a few different ways but I keep getting errors.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Calculating Interest Based On User Inputed Information
16 Sep, 2007 - 08:56 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Your interest rate formula is not correct. You need the balance, annual interest rate and the number of compoundings. Now if you are calculating the straight interest (simple interest) it is simply interest = balance * r where "r" would be the rate as a decimal.

For instance...

CODE

// To calculate 5% of the total principle.
interest = principle * .05


Now something tells me that isn't the formula you need. You are probably looking for one that compounds at a given rate like monthly or quarterly. So when you can tell us that, I am sure we would love to help you further.

Thanks. smile.gif
User is offlineProfile CardPM
+Quote Post

m_milam
RE: Calculating Interest Based On User Inputed Information
16 Sep, 2007 - 09:06 PM
Post #3

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 23


My Contributions
i just need the to figure out what the interest would be on the next monthly payment based on the balance and the annual interest rate being inputted by the user. This is something I have to write for a java class. I had 5 programs to write and this is the final one and i am a stuck point. The formula i provided : interest = balance X (annualInterestRate / 1200) is the formula i was told to use.

thanks for looking at this for me.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Calculating Interest Based On User Inputed Information
16 Sep, 2007 - 09:31 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well ok... I guess they wanted a straight interest formula. So here you go.

CODE

import java.util.Scanner;
import java.text.DecimalFormat;

public class Exercise2_13 {
    /**Main method */
    
    public static void main(String[] args) {
        
        //create a scanner for input
        Scanner input = new Scanner(System.in);
        
        //Enter yearly balance
        System.out.print("Enter Balance: ");
        double Balance = input.nextDouble();
        
        //Enter yearly interest rate
        System.out.print("Enter Annual Interest Rate: ");
        double Interest = input.nextDouble();
        
        //Display results
        System.out.println("The Interest rate is " + Interest);

        // Simple interest formula
        double interestOnBalance = 0.0;
        interestOnBalance = Balance * (Interest / 1200);

        // Format the value into a two place decimal value.
        DecimalFormat form = new DecimalFormat(".00");
        System.out.println("Interest on Balance: $" + form.format(interestOnBalance));
        
        System.exit(0);
  }
}


As you can see I simply plug it into the formula you provided and format the value at the end.

smile.gif
User is offlineProfile CardPM
+Quote Post

m_milam
RE: Calculating Interest Based On User Inputed Information
17 Sep, 2007 - 08:23 PM
Post #5

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 23


My Contributions
Thank you so much. I actually almost had it right in one of my previous versions that did not work at all. I was missing:

[code]
import java.text.DecimalFormat;

// Format the value into a two place decimal value.
DecimalFormat form = new DecimalFormat(".00");
System.out.println("Interest on Balance: $" + form.format(interestOnBalance));
[code]

I was racking my brain on this one. Thanks again for the insight. My program was turned in without this snippet but the main thing is i'm starting to get it.

QUOTE

As you can see I simply plug it into the formula you provided and format the value at the end.

smile.gif


User is offlineProfile CardPM
+Quote Post

evilbert
RE: Calculating Interest Based On User Inputed Information
30 Oct, 2007 - 09:11 AM
Post #6

New D.I.C Head
*

Joined: 30 Oct, 2007
Posts: 1


My Contributions
Anybody know how to work out this when the variable years is involved. This is in a Windows Form. This is C#.

E.g

private void button1_Click(object sender, EventArgs e)
{
string enterDeposit = txtDeposit.Text;
double A = Convert.ToDouble(enterDeposit);

string enterIntrest = txtInterestRate.Text;
double B = Convert.ToDouble(enterIntrest);

string enterYears = txtYears.Text;
double years = Convert.ToDouble(enterYears);

double F; // final total

F = A * (B / 100) + A; // this works it out for 1 year

if (years>1)
{
F = F * (B / 100) + F; // this works it out for 2 years

}

lblOutput.Text = ("Total = " + F);


Problem is when working it out for x amount of years! i.e more than 2
I guess some sort of loop maybe required, maybe for loop

for (years=0; years<1000; years++)
{

CODE

}


Any help would be much appreciated!

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Calculating Interest Based On User Inputed Information
30 Oct, 2007 - 02:48 PM
Post #7

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Try posting this to the C# forum. Myself or one of the other C# gurus there can help you with that. Also do a search on the boards here, I have helped numerous people with implementing the problem using yearly and monthly compoundings based on the X number of years etc.

smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:11AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month