วิธีการคำนวณดอกเบี้ยแบบง่ายและดอกเบี้ยทบต้น

วิธีการคำนวณดอกเบี้ยแบบง่ายและดอกเบี้ยทบต้น

คณิตศาสตร์เป็นส่วนสำคัญของการเขียนโปรแกรม หากคุณไม่สามารถแก้ปัญหาง่ายๆ ในพื้นที่นี้ได้ คุณจะต้องดิ้นรนมากเกินความจำเป็น





โชคดีที่การเรียนรู้วิธีทำนั้นไม่ยากเกินไป ในบทความนี้ คุณจะได้เรียนรู้วิธีคำนวณดอกเบี้ยอย่างง่ายและดอกเบี้ยทบต้นโดยใช้ Python, C++ และ JavaScript





เกมที่จะเล่นเมื่อคุณเบื่อออนไลน์

คุณคำนวณดอกเบี้ยง่าย ๆ ได้อย่างไร?

ดอกเบี้ยอย่างง่ายเป็นวิธีการคำนวณจำนวนดอกเบี้ยที่เรียกเก็บจากจำนวนเงินต้นในอัตราที่กำหนดและในช่วงเวลาที่กำหนด คุณสามารถคำนวณดอกเบี้ยอย่างง่ายโดยใช้สูตรต่อไปนี้:





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

คำชี้แจงปัญหา

คุณได้รับ จำนวนเงินหลัก , อัตราดอกเบี้ย , และ เวลา . คุณต้องคำนวณและพิมพ์ดอกเบี้ยอย่างง่ายสำหรับค่าที่กำหนด ตัวอย่าง : ให้หลักการ = 1,000 อัตรา = 7 และระยะเวลา = 2 ดอกเบี้ยธรรมดา = (หลักการ * อัตรา * ระยะเวลา) / 100 = (1000 * 7 * 2) / 100 = 140 ดังนั้นผลลัพธ์คือ 140

โปรแกรม C++ เพื่อคำนวณดอกเบี้ยแบบง่าย

ด้านล่างนี้คือโปรแกรม C++ เพื่อคำนวณดอกเบี้ยอย่างง่าย:



// 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;
}

เอาท์พุท:

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

ที่เกี่ยวข้อง: วิธีค้นหาปัจจัยทั้งหมดของจำนวนธรรมชาติใน C ++, Python และ JavaScript





โปรแกรม Python คำนวณดอกเบี้ยอย่างง่าย

ด้านล่างนี้คือโปรแกรม Python สำหรับคำนวณดอกเบี้ยอย่างง่าย:

# 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))

เอาท์พุท:





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

ที่เกี่ยวข้อง: วิธีพิชิต FizzBuzz ในภาษาการเขียนโปรแกรมต่างๆ

โปรแกรม JavaScript เพื่อคำนวณดอกเบี้ยแบบง่าย

ด้านล่างนี้คือโปรแกรม JavaScript เพื่อคำนวณดอกเบี้ยอย่างง่าย:

// 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) + '
');

เอาท์พุท:

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

วิธีการคำนวณดอกเบี้ยทบต้น

ดอกเบี้ยทบต้นคือการบวกดอกเบี้ยกับเงินต้น กล่าวอีกนัยหนึ่งก็คือดอกเบี้ยจากดอกเบี้ย คุณสามารถคำนวณดอกเบี้ยทบต้นโดยใช้สูตรต่อไปนี้:

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

คำชี้แจงปัญหา

คุณได้รับ จำนวนเงินหลัก , อัตราดอกเบี้ย , และ เวลา . คุณต้องคำนวณและพิมพ์ดอกเบี้ยทบต้นสำหรับค่าที่กำหนด ตัวอย่าง : ให้หลักการ = 1,000 อัตรา = 7 และระยะเวลา = 2 จำนวน = P (1 + R/100) T = 1144.9 ดอกเบี้ยทบต้น = จำนวนเงิน - จำนวนเงินหลัก = 1144.9 - 1,000 = 144.9 ดังนั้นผลลัพธ์คือ 144.9

โปรแกรม C++ เพื่อคำนวณดอกเบี้ยทบต้น

ด้านล่างนี้คือโปรแกรม C++ เพื่อคำนวณดอกเบี้ยทบต้น:

// 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;
}

เอาท์พุท:

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

ที่เกี่ยวข้อง: วิธีย้อนกลับอาร์เรย์ใน C ++, Python และ JavaScript

ทำไม youtube ของฉันถึงใช้งานไม่ได้

โปรแกรม Python เพื่อคำนวณดอกเบี้ยทบต้น

ด้านล่างนี้คือโปรแกรม Python สำหรับคำนวณดอกเบี้ยทบต้น:

# 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))

เอาท์พุท:

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

ที่เกี่ยวข้อง: วิธีหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

โปรแกรม JavaScript เพื่อคำนวณดอกเบี้ยทบต้น

ด้านล่างนี้คือโปรแกรม JavaScript เพื่อคำนวณดอกเบี้ยทบต้น:

// 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) + '
');

เอาท์พุท:

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

เรียนรู้การเขียนโค้ดฟรี: เริ่มต้นด้วยดอกเบี้ยทบต้น

ทุกวันนี้ผลกระทบของการเข้ารหัสเพิ่มขึ้นอย่างทวีคูณ และด้วยเหตุนี้ ความต้องการสำหรับโปรแกรมเมอร์ที่เชี่ยวชาญก็เพิ่มขึ้นอย่างทวีคูณเช่นกัน มีความเข้าใจผิดในหมู่คนที่พวกเขาสามารถเรียนรู้รหัสหลังจากจ่ายค่าธรรมเนียมสูงเท่านั้น แต่นั่นไม่เป็นความจริง คุณสามารถเรียนรู้การเขียนโค้ดได้ฟรีจากแพลตฟอร์มต่างๆ เช่น freeCodeCamp, Khan Academy, YouTube และอื่นๆ ดังนั้น แม้ว่าคุณจะไม่มีงบประมาณจำนวนมาก คุณก็ไม่ต้องกังวลว่าจะพลาด

แบ่งปัน แบ่งปัน ทวีต อีเมล 7 วิธีที่ดีที่สุดในการเรียนรู้วิธีเขียนโค้ดฟรี

คุณไม่สามารถเรียนรู้การเขียนโค้ดได้ฟรี เว้นแต่คุณจะให้ทรัพยากรที่ทดลองและทดสอบแล้วเหล่านี้แน่นอน

อ่านต่อไป
หัวข้อที่เกี่ยวข้อง
  • การเขียนโปรแกรม
  • Python
  • JavaScript
  • บทเรียนการเข้ารหัส
เกี่ยวกับผู้เขียน ยุวราช จันทรา(60 บทความที่ตีพิมพ์)

Yuvraj เป็นนักศึกษาระดับปริญญาตรีสาขาวิทยาการคอมพิวเตอร์ที่มหาวิทยาลัยเดลี ประเทศอินเดีย เขาหลงใหลเกี่ยวกับ Full Stack Web Development เมื่อไม่ได้เขียน เขากำลังสำรวจความลึกของเทคโนโลยีต่างๆ

เพิ่มเติมจาก Yuvraj Chandra

สมัครรับจดหมายข่าวของเรา

เข้าร่วมจดหมายข่าวของเราสำหรับเคล็ดลับทางเทคนิค บทวิจารณ์ eBook ฟรี และดีลพิเศษ!

คลิกที่นี่เพื่อสมัครสมาชิก