2015-08-08 48 views
-2

问题是这样的 - 银行每月支付曼尼什利息。 Manish也是一个坚定的投资者,并且避免从这个账户中撤回任何东西,因为他现在相信复利的力量。鉴于投资语料库,固定年利率和到期期限计算了马尼什在任期结束时最终节省的金额。这些约束给出:复利计划

P > 0 ; it can be float value, where p is investment corpus 
    R >=0 ; it can be float value, where r is rate of interest per annum 
    T >0 ; it can be integer only, where t is tenure(given in months) 
    Calculation should be done upto 11-digit precision. 

我的代码是这样的:

import java.io.*; 
import java.util.*; 
public class CompoundInterest 
{ 
public static void main(String args[]) 
{ 
float principal; 
int months; 
float rate; 
double final_amount = 0; 
Scanner sc = new Scanner(System.in); 
System.out.println("Principal: \n"); 
principal = sc.nextFloat(); 
System.out.println("Months: \n"); 
months = sc.nextInt(); 
System.out.println("Interest rate: \n"); 
rate = sc.nextFloat(); 
for(int x = 1;x<=months;x++) 
{ 
double amount = principal*Math.pow(1+rate,x); 
final_amount = final_amount+amount; 
} 
System.out.println("final_amount"+final_amount); 
} 
} 

如果我给输入为25,4和6分别..输出应为152 我得到输出任何人都可以帮我纠正我的这个程序并运行这个程序吗?

+0

它是你的任务吗? – Rustam

+0

你的公式看起来不正确。见[这里](http://www.calculatorsoup.com/calculators/financial/compound-interest-calculator.php) – SMA

+0

你可以问你的老师,如何计算float值可能达到11位精度。 –

回答

2

随着您的程序和您的输入,我得到7000结果。

请记住,使用6作为率你使用它的方式实际上意味着600%。

你也必须考虑。费率是每月还是每年?根据您的答案,您可能需要将其除以12。提示:请阅读您问题的第二行。

另外。你从哪里得到答案是从152?
没有做任何数学计算,很明显,在6%年利率下25美元在四个月内永远不会变成152美元。