Java 程序:计算复利
原文:https://www.studytonight.com/java-programs/java-program-to-calculate-compound-interest
在本教程中,我们将学习如何在给定本金、利率、时间段和复利次数的情况下找到复利。但是在继续之前,如果你不熟悉 java 中算术运算符的概念,那么一定要查看关于 Java 中运算符的文章。
输入:输入本金金额:6200.0
输入汇率:11.0
输入时间段:2.0
输出:
复利:886600.0
2 年期末金额:892800.0
上述问题可以通过以下方式解决:
方法 1:当值由用户定义时
方法 2:当值被预定义时
让我们分别看看这些方法。
程序 1:计算复利
在这个程序中,我们将看到当值是用户定义的时,如何使用公式找到复利。这意味着,首先我们将要求用户初始化变量,然后我们将使用公式找到复利。
算法:
- 开始
- 创建 Scanner 类的实例,从用户处获取输入。
- 声明本金金额、利率、时间段和复利次数的变量。
- 要求用户初始化这些变量。
- 使用公式计算复利。
- 打印复利的价值。
- 打印复利后的金额。
- 停止
下面是相同的代码。
//Java Program to calculate the compound interest
import java.util.*;
public class Main
{
public static void main(String args[])
{
//Take input from the user
//Create an instance of the Scanner class
Scanner sc = new Scanner(System.in);
//Declare variables
float p, r, t, n;
System.out.println("Enter the Principal : ");
p = sc.nextFloat(); //Initialize the variables
System.out.println("Enter the Rate of interest : ");
r = sc.nextFloat(); //Initialize the variables
System.out.println("Enter the Time period : ");
t = sc.nextFloat(); //Initialize the variables
System.out.println("Enter the number of times that interest is compounded per unit t");
n=sc.nextFloat(); //Initialize the variables
sc.close();
//Calculate the compound interest
double amount = p * Math.pow(1 + (r / n), n * t);
double cinterest = amount - p;
System.out.println("Compound Interest after " + t + " years: "+cinterest);
System.out.println("Amount after " + t + " years: "+amount);
}
}
输入本金金额:5200 输入利率:12 输入时间段:3 输入单位 t 复利次数:2 3.0 年后复利:6.117696 E8 3.0 年后金额:6.117748E8
程序 2:计算复利
在这个程序中,我们将看到当值在程序中预先定义时,如何使用公式找到复利。
算法:
- 开始
- 创建 Scanner 类的实例,从用户处获取输入。
- 声明本金金额、利率、时间段和复利次数的变量。
- 初始化这些变量。
- 使用公式计算复利。
- 打印复利的价值。
- 打印复利后的金额。
- 停止
下面是相同的代码。
//Java Program to calculate the compound interest
public class Main
{
public static void main(String args[])
{
//Declare and initialize the variables
float p = 4500, r = 10, t = 2 , n=1;
//Print the variables and their corresponding values
System.out.println("The entered principle amount is = " + p);
System.out.println("The entered rate is = " + r);
System.out.println("The entered time period is " + t);
System.out.println("The entered number of times the interest is compounded is " + n);
//Calculate the compound interest and the amount
double amount = p * Math.pow(1 + (r / n), n * t);
double cinterest = amount - p;
System.out.println("Compound Interest after " + t + " years: "+cinterest);
System.out.println("Amount after " + t + " years: "+amount);
}
}
录入本金金额为= 4500.0 录入利率为= 10.0 录入时间段为 2.0 录入复利次数为 1.0 2.0 年后复利:540000.0 2.0 年后金额:544500.0
程序 3:寻找复利
在这个程序中,我们将看到当值是用户定义的时,如何使用公式找到复利。这意味着,首先我们将首先要求用户初始化变量,然后用用户定义的方法计算复利。
算法:
- 开始
- 创建 Scanner 类的实例,从用户处获取输入。
- 声明本金金额、利率、时间段和复利次数的变量。
- 要求用户初始化这些变量。
- 调用一个方法来计算复利。
- 使用公式计算复利。
- 打印复利的价值。
- 打印复利后的金额。
- 停止
下面是相同的代码。
//Java Program to calculate the compound interest
public class Main
{
public static void main(String args[])
{
//Declare and initialize the variables
float p = 2900, r = 18, t = 2 , n=1;
//Print the variables and their corresponding values
System.out.println("The entered principle amount is = " + p);
System.out.println("The entered rate is = " + r);
System.out.println("The entered time period is " + t);
System.out.println("The entered number of times the interest is compounded is " + n);
findCi(p,r,t,n);
}
public static void findCi(float p, float r, float t, float n)
{
//Calculate the compound interest and the amount
double amount = p * Math.pow(1 + (r / n), n * t);
double cinterest = amount - p;
System.out.println("Compound Interest after " + t + " years: "+cinterest);
System.out.println("Amount after " + t + " years: "+amount);
}
}
录入本金金额为= 2900.0 录入利率为= 18.0 录入时间段为 2.0 录入复利次数为 1.0 2.0 年后复利:1044000.0 2.0 年后金额:1046900.0