Devdit
 

วิธีเขียนโปรแกรม C++ คำนวณภาษีเงินได้

2.4K

บทความนี้สอนวิธีเขียนโปรแกรม C++ คำนวณภาษีเงินได้แบบบุคคลธรรมดาในประเทศไทย แบบคิดอัตราภาษีแบบขั้นบันได โดยโปรแกรมนี้จะมีการรับค่าเงินได้ (income) ค่าใช้จ่าย (expenses) และค่าลดหย่อน (deductions) และนำมาคำนวณพร้อมแสดงผลลัพธ์ภาษี

 

ตัวอย่าง สูตรคำนวณหาเงินได้สุทธิ และภาษีที่ต้องจ่าย

1. เงินได้สุทธิ จะเกี่ยวข้องกับค่าเงินได้ ค่าใช้จ่าย และค่าลดหย่อน

เงินได้สุทธิ = เงินได้ - ค่าใช้จ่าย - ค่าลดหย่อน
net_income = income - expenses - deductions

 

2. ภาษีที่ต้องจ่าย จะเกี่ยวข้องกับ เงินได้สุทธิ และอัตราภาษี

ภาษีที่ต้องจ่าย = เงินได้สุทธิ x อัตราภาษี (อัตราภาษีตามตารางด้านล่าง)
tax = net_income x tax

 

ตัวอย่าง ตารางวิธีคำนวณภาษีเงินได้บุคคลธรรมดา แบบคิดอัตราภาษีแบบขั้นบันได

เงินได้สุทธิ (บาท)

อัตราภาษี

1 - 150,000

ได้รับยกเว้น

150,001 - 300,000

5%

300,001 - 500,000

10%

500,001 - 750,000

15%

750,001 - 1,000,000

20%

1,000,0001 - 2,000,000

25%

2,000,001 - 5,000,000

30%

5,000,001 บาทขึ้นไป

35%

 

ตัวอย่าง วิธีเขียนโปรแกรม C++ คำนวณภาษีเงินได้

#include <iostream>

using namespace std;

int main() {

  double income = 0.0;
  cout << "Enter your net income: ";
  cin >> income;

  double expenses = 0.0;
  cout << "Enter your expenses: ";
  cin >> expenses;

  double deductions = 0.0;
  cout << "Enter your deductions: ";
  cin >> deductions;

  double net_income = income - expenses - deductions;

  double tax = 0.0;
  if (net_income <= 150000) {
    tax = 0;
  } else if (net_income <= 300000) {
    tax = net_income * 0.05;
  } else if (net_income <= 500000) {
    tax = net_income * 0.1;
  } else if (net_income <= 750000) {
    tax = net_income * 0.15;
  } else if (net_income <= 1000000) {
    tax = net_income * 0.20;
  } else if (net_income <= 2000000) {
    tax = net_income * 0.25;
  } else if (net_income <= 5000000) {
    tax = net_income * 0.30;
  } else {
    tax = net_income * 0.35;
  }

  cout << "Your net income is: " << net_income << " THB" << endl;
  cout << "Your tax is: " << tax << " THB" << endl;

  return 0;
}
ตัวอย่างที่ 1.
Enter your net income: 100000
Enter your expenses: 1000
Enter your deductions: 500
Your net income is: 98,500 THB
Your tax is: 0 THB
ตัวอย่างที่ 2.
Enter your net income: 550000
Enter your expenses: 10000
Enter your deductions: 10000
Your net income is: 530,000 THB
Your tax is: 79,500 THB

จากตัวอย่างวิธีเขียนโปรแกรม C++ คำนวณภาษีเงินได้ และผลลัพธ์สามารถอธิบายได้ดังนี้

1. รับค่าเงินได้ เก็บไว้ที่ตัวแปร income

2. รับค่าใช้จ่าย เก็บไว้ที่ตัวแปร expenses

3. รับค่าลดหย่อน เก็บไว้ที่ตัวแปร deductions

4. หาเงินได้สุทธิ = เงินได้ - ค่าใช้จ่าย - ค่าลดหย่อน เท่ากับ net_income = income - expenses - deductions

5. นำเงินได้สุทธิ (net_income) ไปเข้าเงื่อนไขคำนวณภาษี

6. กรณีเงินได้สุทธิ (net_income) = 98,500 ทำงานเงื่อนไข if (net_income <= 150000) ภาษี (tax) = 0

7. กรณีเงินได้สุทธิ (net_income) = 530,000 ทำงานเงื่อนไข else if (net_income <= 750000) ภาษี (tax) = 79,500

8. แสดงค่าเงินได้สุทธิ (net_income) และภาษี (tax) ออกสู่หน้าจอ

 

สรุป บทความนี้สอนวิธีเขียนโปรแกรม C++ คำนวณภาษีเงินได้ โดยเริ่มจากเข้าใจสูตรวิธีคำนวณภาษีเงินได้สุทธิ และภาษีที่ต้องจ่าย พร้อมกับข้อมูลตารางอัตราภาษีแบบขั้นบันได และทดลองเขียนโปรแกรมคำนวณภาษีเงินได้ด้วยภาษา C++ พร้อมแสดงผลลัพธ์ และคำอธิบายโค้ดโปรแกรม

แก้ไข 8 เดือนที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ