In: Computer Science
java Problem 3: An Interesting Problem
Write a program that accepts two positive integers: a deposited
amount of money and an
interest rate, as an annual percentage rate. Your program will
calculate the number of years that
will take for the account balance to reach $1, 000,000. You can
assume that the initial deposit is
less than $1,000,000
Input
The input will begin with a single line containing T , the number
of test cases to follow. The
remaining lines contain the lines to be calculated. Each of these
lines has two positive integers
separated by a single space. The first value is the deposited
amount, the second is the interest
rate.
Output
The output should consist of the number of years.
Sample Input Sample output
2 cases
10000 10 49 Years
500 5 156 years
CODE IN JAVA:
Interest.java file:
import java.util.Scanner;
public class Interest {
   public static void main(String[] args) {
       // declaring variables
       double
initialAmount,interestRate,interest;
       int n;
       //taking inputs
       Scanner sc = new
Scanner(System.in);
       System.out.print("Enter the number
of test cases:");
       n = sc.nextInt();
       for(int i=1;i<=n;i++) {
          
System.out.print("Enter the initial amount and interest
rate:");
           initialAmount =
sc.nextDouble();
           interestRate =
sc.nextDouble();
           int count =
0;
           //calculating
the number of years needed
          
while(initialAmount < 1000000) {
          
    interest = (initialAmount *
interestRate)/100;
          
    initialAmount+=interest;
          
    count += 1 ;
           }
           //displaying the
result
          
System.out.println("Total years needed:"+count);
       }
   }
}
OUTPUT:
