Following Code will calculate the number of years required to get Desired Amount in an Investment
Code::
import java.util.*;
public class Investment
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter Your Desired Amount");
int a=in.nextInt();
System.out.println("Enter Amount to Contribute Every year");
int b=in.nextInt();
System.out.println("Enter Rate of Interest in percent");
int r=in.nextInt();
int amount=0,year=0,interest=0;
while(amount<a)
{
amount=amount+b;
interest=amount*r/100;
amount=amount+interest;
year++;
}
System.out.printf("You will get Amount=%d in %d years\n",amount,year);
}
}
Output:
Code::
import java.util.*;
public class Investment
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter Your Desired Amount");
int a=in.nextInt();
System.out.println("Enter Amount to Contribute Every year");
int b=in.nextInt();
System.out.println("Enter Rate of Interest in percent");
int r=in.nextInt();
int amount=0,year=0,interest=0;
while(amount<a)
{
amount=amount+b;
interest=amount*r/100;
amount=amount+interest;
year++;
}
System.out.printf("You will get Amount=%d in %d years\n",amount,year);
}
}
Output: