Skip to main content

Simple stuff

1 reply [Last post]
Joined: 2013-02-06
Points: 0

Brand new to java and programming for the most part and am taking a class just for the hell of it. But anyways my professor told us to make a simple program for calculating interest.

I've got it all built and to where it runs correctly except that the calculation for the interest is coming up to high.

public class Interest2 {
public static void main(String[] args){

double amount = 5000;
double interest = 0;
int years = 15;
int rate = 6;

interest = amount * (rate%100) * years;

System.out.println(" The amount is " +amount
+ "\nThe interest is " +interest
+ "\nThe total years is " +years
+ "\nThe rate is " +rate);


That's what I've written for it, like I said very basic stuff..But when I run the program the output is
The amount is 5000.0
The interest is 450000.0
The total years is 15
The rate is 6

That 450,000 is suppose to be 4500 and I can't figure out what's going on with it, anyone able to help?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2013-02-09
Points: 0

Hi Councillor

First, please use code tags when posting code samples.

I see two issues with your code. the first one is you are using the modulus instead of division to get the rate percent. Modulus only captures the remainder. You'll want to switch from % to / like so:

Interest = amount * (rate/100) * years;

You will also need to declare rate as a double instead of and int so the fraction from the division by 100 doesn't get truncated and end up 0 instead of 0.06.

Give that a try.