# Thread: How to detect when 12 months have passed by

1. Senior 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 12-month-period 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);
}
}```
Before I can test it, I need to figure out how to dynamically keep track of how many times 12 months have gone by in the if statement of my for loop on line 31. The problem with the way it currently is is that I might reach 24 months, 36 months, and so on.

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?

2. ## Re: How to detect when 12 months have passed by

some kind of trick with modulus,
Try writing a test program with a loop that prints out the modulus value to see how it works:
Java Code:
`   System.out.println("aNumber="+ aNumber + " % anotherNumber= " + anotherNumber + " = "+ (aNumber % anotherNumber));`
Can you post an example of some sample input and the desired output?
Last edited by Norm; 08-09-2019 at 02:47 PM.

3. Senior 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);
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•