Results 1 to 3 of 3
 08092019, 08:52 AM #1Senior Member
 Join Date
 Jul 2013
 Location
 Wisconsin, USA
 Posts
 106
 Rep Power
 0
How to detect when 12 months have passed by
I have another simple programming assignment. I need to calculate accumulated interest, but I also need to keep track of how many times a 12monthperiod passes by.
Here's the code so far:
Java Code:package com.sam; import java.util.Scanner; public class MainMethodPlayground { private static final String correctMain = "The main method of the MainMethodPlayground class is being used.\n"; public static void main(String[] args) { System.out.println(correctMain); Scanner intInput = new Scanner(System.in); Scanner dblInput = new Scanner(System.in); /* 13. Develop a program that accepts an initial amount of money (called the principal), a simple annual interest rate, and a number of months will compute the balance at the end of that time. Assume that no additional deposits or withdrawals are made and that a month is 1/12 of a year. Total interest is the product of the principal, the annual interest rate expressed as a decimal, and the number of years. */ System.out.println("Enter an initial amount of money:"); double principal = dblInput.nextDouble(); System.out.println("Enter an annual interest rate:"); double interestRate = dblInput.nextDouble(); System.out.println("Enter number of months to accumulate interest:"); int numMonths = intInput.nextInt(); double years = 0; double sumOfInterest = 0; for(int i = 0; i < numMonths; i++) { sumOfInterest += (interestRate*principal); if(i == 12) { System.out.println("Interest has accumulated for another full year."); } } System.out.println("After " + years + " years, we have accumulated $" + sumOfInterest); } }
1/12 = 0.0833333333333333 doesn't look very clean, and it wouldn't look proper to print out something like:
"After 1.0833333333333333 years the interest is..."//13 months
"After 1..1666666666666666 years, the interest is..."//14 months
Isn't there a better way involving some kind of trick with modulus, where you see if the quotient of a fraction has a remainder of 0, which we could use to detect when i has incremented a number divisible by 12 or something? I can't remember how that kind of trick works and I'm not sure how to explain it well enough to figure out what to google search for. Any ideas?
 08092019, 01:47 PM #2
Re: How to detect when 12 months have passed by
some kind of trick with modulus,Java Code:System.out.println("aNumber="+ aNumber + " % anotherNumber= " + anotherNumber + " = "+ (aNumber % anotherNumber));
Last edited by Norm; 08092019 at 02:47 PM.
If you don't understand my response, don't ignore it, ask a question.
 08092019, 11:15 PM #3Senior Member
 Join Date
 Jul 2013
 Location
 Wisconsin, USA
 Posts
 106
 Rep Power
 0
Re: How to detect when 12 months have passed by
I figured it out:
Java Code:System.out.println("Enter an initial amount of money:"); double principal = dblInput.nextDouble(); System.out.println("Enter an annual interest rate:"); double interestRate = dblInput.nextDouble(); System.out.println("Enter number of months to accumulate interest:"); int numMonths = intInput.nextInt(); int i = 0; int years = 0; double sumOfInterest = 0; for(i = 0; i < numMonths; i++) { sumOfInterest += (interestRate*principal); if(i % 12 == 0) { years++; } } if(numMonths % 12 == 0) {//if n years and 0 months have gone by System.out.println("After " + years + " years, we have accumulated $" + sumOfInterest + " of interest."); } else if((numMonths > 12) && (numMonths % 12 != 0)) {//if n years and n months have gone by System.out.println("After " + years + " years and " + (numMonths % 12) + " months, we have accumulated $" + sumOfInterest + " of interest."); } else if(numMonths < 12){//if less than a year has gone by System.out.println("After " + numMonths + " months, we have accumulated $" + sumOfInterest); }
Similar Threads

Somebody please help me! You must know how! This has been plauging me for months!
By I Want To Kill Java in forum Forum LobbyReplies: 2Last Post: 12292010, 06:30 AM 
entities are passed by value or passed by reference
By syntrax in forum New To JavaReplies: 1Last Post: 12172009, 07:13 AM
Bookmarks