Jak obliczyć odsetki proste i składane

Jak obliczyć odsetki proste i składane

Matematyka jest integralną częścią programowania. Jeśli nie potrafisz rozwiązać prostych problemów w tej dziedzinie, będziesz się zmagać o wiele bardziej, niż jest to konieczne.





Na szczęście nauka tego nie jest zbyt trudna. W tym artykule dowiesz się, jak obliczać odsetki proste i składane za pomocą Pythona, C++ i JavaScriptu.





dodaj plan zajęć do kalendarza google

Jak obliczyć proste oprocentowanie?

Odsetki proste to metoda obliczania kwoty odsetek naliczanych od kwoty głównej przy danej stopie i za dany okres czasu. Proste oprocentowanie można obliczyć za pomocą następującego wzoru:





Simple Interest = (P x R x T)/100
Where,
P = Principle Amount
R = Rate
T = Time

Stwierdzenie problemu

masz zasadnicza kwota , stopa procentowa , oraz czas . Musisz obliczyć i wydrukować proste odsetki dla podanych wartości. Przykład : Zasada Let = 1000, stopa = 7, a timePeriod = 2. Prosty odsetek = (zasada * stopa * timePeriod) / 100 = (1000 * 7 * 2) / 100 = 140. Zatem wynik wynosi 140.

Program C++ do obliczania prostych odsetek

Poniżej znajduje się program C++ do obliczania odsetek prostych:



// C++ program to calculate simple interest
// for given principle amount, time, and rate of interest.
#include
using namespace std;
// Function to calculate simple interest
float calculateSimpleInterest(float principle, float rate, float timePeriod)
{
return (principle * rate * timePeriod) / 100;
}

int main()
{
float principle1 = 1000;
float rate1 = 7;
float timePeriod1 = 2;
cout << 'Test case: 1' << endl;
cout << 'Principle amount: ' << principle1 << endl;
cout << 'Rate of interest: ' << rate1 << endl;
cout << 'Time period: ' << timePeriod1 << endl;
cout << 'Simple Interest: ' << calculateSimpleInterest(principle1, rate1, timePeriod1) << endl;
float principle2 = 5000;
float rate2 = 5;
float timePeriod2 = 1;
cout << 'Test case: 2' << endl;
cout << 'Principle amount: ' << principle2 << endl;
cout << 'Rate of interest: ' << rate2 << endl;
cout << 'Time period: ' << timePeriod2 << endl;
cout << 'Simple Interest: ' << calculateSimpleInterest(principle2, rate2, timePeriod2) << endl;
float principle3 = 5800;
float rate3 = 4;
float timePeriod3 = 6;
cout << 'Test case: 3' << endl;
cout << 'Principle amount: ' << principle3 << endl;
cout << 'Rate of interest: ' << rate3 << endl;
cout << 'Time period: ' << timePeriod3 << endl;
cout << 'Simple Interest: ' << calculateSimpleInterest(principle3, rate3, timePeriod3) << endl;
return 0;
}

Wyjście:

Test case: 1
Principle amount: 1000
Rate of interest: 7
Time period: 2
Simple Interest: 140
Test case: 2
Principle amount: 5000
Rate of interest: 5
Time period: 1
Simple Interest: 250
Test case: 3
Principle amount: 5800
Rate of interest: 4
Time period: 6
Simple Interest: 1392

Powiązane: Jak znaleźć wszystkie czynniki liczby naturalnej w C++, Pythonie i JavaScript





Program Pythona do obliczania prostych odsetek

Poniżej znajduje się program Pythona do obliczania prostych odsetek:

# Python program to calculate simple interest
# for given principle amount, time, and rate of interest.
# Function to calculate simple interest
def calculateSimpleInterest(principle, rate, timePeriod):
return (principle * rate * timePeriod) / 100

principle1 = 1000
rate1 = 7
timePeriod1 = 2
print('Test case: 1')
print('Principle amount:', principle1)
print('Rate of interest:', rate1)
print('Time period:', timePeriod1)
print('Simple Interest:', calculateSimpleInterest(principle1, rate1, timePeriod1))
principle2 = 5000
rate2 = 5
timePeriod2 = 1
print('Test case: 2')
print('Principle amount:', principle2)
print('Rate of interest:', rate2)
print('Time period:', timePeriod2)
print('Simple Interest:', calculateSimpleInterest(principle2, rate2, timePeriod2))
principle3 = 5800
rate3 = 4
timePeriod3 = 6
print('Test case: 3')
print('Principle amount:', principle3)
print('Rate of interest:', rate3)
print('Time period:', timePeriod3)
print('Simple Interest:', calculateSimpleInterest(principle3, rate3, timePeriod3))

Wyjście:





Test case: 1
Principle amount: 1000
Rate of interest: 7
Time period: 2
Simple Interest: 140.0
Test case: 2
Principle amount: 5000
Rate of interest: 5
Time period: 1
Simple Interest: 250.0
Test case: 3
Principle amount: 5800
Rate of interest: 4
Time period: 6
Simple Interest: 1392.0

Związane z: Jak ukończyć wyzwanie FizzBuzz w różnych językach programowania?

Program JavaScript do obliczania prostych odsetek

Poniżej znajduje się program JavaScript do obliczania prostych odsetek:

// JavaScript program to calculate simple interest
// for given principle amount, time, and rate of interest.
// Function to calculate simple interest
function calculateSimpleInterest(principle, rate, timePeriod) {
return (principle * rate * timePeriod) / 100;
}
var principle1 = 1000;
var rate1 = 7;
var timePeriod1 = 2;
document.write('Test case: 1' + '
');
document.write('Principle amount: ' + principle1 + '
');
document.write('Rate of interest: ' + rate1 + '
');
document.write('Time period: ' + timePeriod1 + '
');
document.write('Simple Interest: ' + calculateSimpleInterest(principle1, rate1, timePeriod1) + '
');
var principle2 = 5000;
var rate2 = 5;
var timePeriod2 = 1;
document.write('Test case: 2' + '
');
document.write('Principle amount: ' + principle2 + '
');
document.write('Rate of interest: ' + rate2 + '
');
document.write('Time period: ' + timePeriod2 + '
');
document.write('Simple Interest: ' + calculateSimpleInterest(principle2, rate2, timePeriod2) + '
');
var principle3 = 5800;
var rate3 = 4;
var timePeriod3 = 6;
document.write('Test case: 3' + '
');
document.write('Principle amount: ' + principle3 + '
');
document.write('Rate of interest: ' + rate3 + '
');
document.write('Time period: ' + timePeriod3 + '
');
document.write('Simple Interest: ' + calculateSimpleInterest(principle3, rate3, timePeriod3) + '
');

Wyjście:

Test case: 1
Principle amount: 1000
Rate of interest: 7
Time period: 2
Simple Interest: 140
Test case: 2
Principle amount: 5000
Rate of interest: 5
Time period: 1
Simple Interest: 250
Test case: 3
Principle amount: 5800
Rate of interest: 4
Time period: 6
Simple Interest: 1392

Jak obliczyć odsetki składane

Odsetki składane to dodanie odsetek do kwoty głównej. Innymi słowy, to odsetki od odsetek. Oprocentowanie składane można obliczyć za pomocą następującego wzoru:

Amount= P(1 + R/100)T
Compound Interest = Amount – P
Where,
P = Principle Amount
R = Rate
T = Time

Stwierdzenie problemu

masz zasadnicza kwota , stopa procentowa , oraz czas . Musisz obliczyć i wydrukować odsetki składane dla podanych wartości. Przykład : Zasada Let = 1000, stopa = 7 i timePeriod = 2. Kwota = P(1 + R/100)T = 1144,9 Odsetki złożone = Kwota - Zasada Kwota = 1144,9 - 1000 = 144,9 Zatem wynik wynosi 144,9.

Program C++ do obliczania odsetek składanych

Poniżej znajduje się program C++ do obliczania odsetek składanych:

// C++ program to calculate compound interest
// for given principle amount, time, and rate of interest.
#include
using namespace std;
// Function to calculate compound interest
float calculateCompoundInterest(float principle, float rate, float timePeriod)
{
double amount = principle * (pow((1 + rate / 100), timePeriod));
return amount - principle;
}
int main()
{
float principle1 = 1000;
float rate1 = 7;
float timePeriod1 = 2;
cout << 'Test case: 1' << endl;
cout << 'Principle amount: ' << principle1 << endl;
cout << 'Rate of interest: ' << rate1 << endl;
cout << 'Time period: ' << timePeriod1 << endl;
cout << 'Compound Interest: ' << calculateCompoundInterest(principle1, rate1, timePeriod1) << endl;
float principle2 = 5000;
float rate2 = 5;
float timePeriod2 = 1;
cout << 'Test case: 2' << endl;
cout << 'Principle amount: ' << principle2 << endl;
cout << 'Rate of interest: ' << rate2 << endl;
cout << 'Time period: ' << timePeriod2 << endl;
cout << 'Compound Interest: ' << calculateCompoundInterest(principle2, rate2, timePeriod2) << endl;
float principle3 = 5800;
float rate3 = 4;
float timePeriod3 = 6;
cout << 'Test case: 3' << endl;
cout << 'Principle amount: ' << principle3 << endl;
cout << 'Rate of interest: ' << rate3 << endl;
cout << 'Time period: ' << timePeriod3 << endl;
cout << 'Compound Interest: ' << calculateCompoundInterest(principle3, rate3, timePeriod3) << endl;
return 0;
}

Wyjście:

Test case: 1
Principle amount: 1000
Rate of interest: 7
Time period: 2
Compound Interest: 144.9
Test case: 2
Principle amount: 5000
Rate of interest: 5
Time period: 1
Compound Interest: 250
Test case: 3
Principle amount: 5800
Rate of interest: 4
Time period: 6
Compound Interest: 1538.85

Związane z: Jak odwrócić tablicę w C++, Pythonie i JavaScript

czy ps4 jest wstecznie kompatybilny z ps3?

Program Pythona do obliczania odsetek składanych

Poniżej znajduje się program Pythona do obliczania odsetek składanych:

# Python program to calculate compound interest
# for given principle amount, time, and rate of interest.
# Function to calculate compound interest
def calculateCompoundInterest(principle, rate, timePeriod):
amount = principle * (pow((1 + rate / 100), timePeriod))
return amount - principle
principle1 = 1000
rate1 = 7
timePeriod1 = 2
print('Test case: 1')
print('Principle amount:', principle1)
print('Rate of interest:', rate1)
print('Time period:', timePeriod1)
print('Compound Interest:', calculateCompoundInterest(principle1, rate1, timePeriod1))
principle2 = 5000
rate2 = 5
timePeriod2 = 1
print('Test case: 2')
print('Principle amount:', principle2)
print('Rate of interest:', rate2)
print('Time period:', timePeriod2)
print('Compound Interest:', calculateCompoundInterest(principle2, rate2, timePeriod2))
principle3 = 5800
rate3 = 4
timePeriod3 = 6
print('Test case: 3')
print('Principle amount:', principle3)
print('Rate of interest:', rate3)
print('Time period:', timePeriod3)
print('Compound Interest:', calculateCompoundInterest(principle3, rate3, timePeriod3))

Wyjście:

Test case: 1
Principle amount: 1000
Rate of interest: 7
Time period: 2
Compound Interest: 144.9000000000001
Test case: 2
Principle amount: 5000
Rate of interest: 5
Time period: 1
Compound Interest: 250.0
Test case: 3
Principle amount: 5800
Rate of interest: 4
Time period: 6
Compound Interest: 1538.8503072768026

Związane z: Jak znaleźć sumę wszystkich elementów w tablicy

Program JavaScript do obliczania odsetek składanych

Poniżej znajduje się program JavaScript do obliczania odsetek składanych:

// JavaScript program to calculate compound interest
// for given principle amount, time, and rate of interest.

// Function to calculate compound interest
function calculateCompoundInterest(principle, rate, timePeriod) {
var amount = principle * (Math.pow((1 + rate / 100), timePeriod));
return amount - principle;
}
var principle1 = 1000;
var rate1 = 7;
var timePeriod1 = 2;
document.write('Test case: 1' + '
');
document.write('Principle amount: ' + principle1 + '
');
document.write('Rate of interest: ' + rate1 + '
');
document.write('Time period: ' + timePeriod1 + '
');
document.write('Compound Interest: ' + calculateCompoundInterest(principle1, rate1, timePeriod1) + '
');
var principle2 = 5000;
var rate2 = 5;
var timePeriod2 = 1;
document.write('Test case: 2' + '
');
document.write('Principle amount: ' + principle2 + '
');
document.write('Rate of interest: ' + rate2 + '
');
document.write('Time period: ' + timePeriod2 + '
');
document.write('Compound Interest: ' + calculateCompoundInterest(principle2, rate2, timePeriod2) + '
');
var principle3 = 5800;
var rate3 = 4;
var timePeriod3 = 6;
document.write('Test case: 3' + '
');
document.write('Principle amount: ' + principle3 + '
');
document.write('Rate of interest: ' + rate3 + '
');
document.write('Time period: ' + timePeriod3 + '
');
document.write('Compound Interest: ' + calculateCompoundInterest(principle3, rate3, timePeriod3) + '
');

Wyjście:

Test case: 1
Principle amount: 1000
Rate of interest: 7
Time period: 2
Compound Interest: 144.9000000000001
Test case: 2
Principle amount: 5000
Rate of interest: 5
Time period: 1
Compound Interest: 250
Test case: 3
Principle amount: 5800
Rate of interest: 4
Time period: 6
Compound Interest: 1538.8503072768008

Naucz się kodować za darmo: zacznij od prostych i złożonych odsetek

Obecnie wpływ kodowania rośnie wykładniczo. W związku z tym gwałtownie rośnie również zapotrzebowanie na biegłych programistów. Wśród ludzi panuje błędne przekonanie, że mogą nauczyć się kodować tylko po uiszczeniu wysokiej opłaty. Ale to nieprawda. Możesz nauczyć się kodować całkowicie za darmo z platform takich jak freeCodeCamp, Khan Academy, YouTube i tak dalej. Tak więc, nawet jeśli nie masz dużego budżetu, nie musisz się martwić, że stracisz.

Udział Udział Ćwierkać E-mail 7 najlepszych sposobów na naukę kodowania za darmo

Nie możesz nauczyć się kodować za darmo. O ile oczywiście nie wypróbujesz tych wypróbowanych i przetestowanych zasobów.

Czytaj dalej
Powiązane tematy
  • Programowanie
  • Pyton
  • JavaScript
  • Poradniki kodowania
O autorze Yuvraj Chandra(60 opublikowanych artykułów)

Yuvraj jest studentem informatyki na Uniwersytecie w Delhi w Indiach. Jest pasjonatem Full Stack Web Development. Kiedy nie pisze, bada głębię różnych technologii.

Więcej od Yuvraja Chandra

Zapisz się do naszego newslettera

Dołącz do naszego newslettera, aby otrzymywać porady techniczne, recenzje, bezpłatne e-booki i ekskluzywne oferty!

Kliknij tutaj, aby zasubskrybować