Простые и сложные проценты в Java — программа расчёта по формуле

Простой процент (simple interest) начисляется только на изначальную сумму. Сложный процент (compound interest) начисляется на сумму с учётом уже начисленных процентов. Ниже — обе формулы в коде.

Пример 1: расчёт простых процентов в Java

import java.util.Scanner;

class Main {
  public static void main(String[] args) {

    // create an object of Scanner class
    Scanner input = new Scanner(System.in);

    // take input from users
    System.out.print("Enter the principal: ");
    double principal = input.nextDouble();

    System.out.print("Enter the rate: ");
    double rate = input.nextDouble();

    System.out.print("Enter the time: ");
    double time = input.nextDouble();

    double interest = (principal * time * rate) / 100;

    System.out.println("Principal: " + principal);
    System.out.println("Interest Rate: " + rate);
    System.out.println("Time Duration: " + time);
    System.out.println("Simple Interest: " + interest);

    input.close();
  }
}

Вывод:

Enter the principal: 1000
Enter the rate: 8
Enter the time: 2
Principal: 1000.0
Interest Rate: 8.0
Time Duration: 2.0
Simple Interest: 160.0

В этом примере с помощью класса Scanner мы получаем от пользователя principal (сумму вклада), rate (ставку) и time (время). Затем по формуле простых процентов считаем результат.

Примечание

Формула простых процентов:

Simple Interest = (Principal * Rate * Time) / 100

Пример 2: расчёт сложных процентов

import java.util.Scanner;

class Main {
  public static void main(String[] args) {

    // create an object of Scanner class
    Scanner input = new Scanner(System.in);

    // take input from users
    System.out.print("Enter the principal: ");
    double principal = input.nextDouble();

    System.out.print("Enter the rate: ");
    double rate = input.nextDouble();

    System.out.print("Enter the time: ");
    double time = input.nextDouble();

    System.out.print("Enter number of times interest is compounded: ");
    int number = input.nextInt();

    double interest = principal * (Math.pow((1 + rate/100), (time * number))) - principal;

    System.out.println("Principal: " + principal);
    System.out.println("Interest Rate: " + rate);
    System.out.println("Time Duration: " + time);
    System.out.println("Number of Time interest Compounded: " + number);
    System.out.println("Compound Interest: " + interest);

    input.close();
  }
}

Вывод:

Enter the principal: 1000
Enter the rate: 10
Enter the time: 3
Enter number of times interest is compounded: 1
Principal: 1000.0
Interest Rate: 10.0
Time Duration: 3.0
Number of Time interest Compounded: 1
Compound Interest: 331.00000000000045

В этом примере мы использовали формулу сложных процентов. Для возведения в степень был применён метод Math.pow().