Gross Salary Calculation Program in C सीखना हर C programming beginner के लिए एक जरूरी milestone है, क्योंकि यह program आपको real-world logic को code में translate करना सिखाता है। इस post में हम step-by-step एक ऐसा program बनाएंगे जो user से Basic Salary लेता है, HRA और DA calculation करता है, और अंत में Gross Salary display करता है।
यह exercise सिर्फ एक simple program नहीं है — यह आपको C language के कुछ सबसे important concepts जैसे if-else conditions, float variables, percentage calculation, और formatted output की practical understanding देती है। अगर आप C programming में नए हैं, तो यह program आपके foundation को मजबूत बनाने के लिए perfect है।
इस blog post को पढ़ने के बाद आप जानेंगे कि program का complete code कैसा दिखता है, हर line का क्या मतलब है, output कैसा आएगा, और किन common mistakes से बचना चाहिए। तो चलिए शुरू करते हैं!
Table of Contents
Gross Salary Calculation Program in C — पूरी जानकारी
C language में salary calculator बनाना beginners के लिए एक ideal practice problem है। इसमें आप arithmetic operations, conditional logic, और user input — तीनों एक साथ use करते हैं। आइए पहले समझते हैं कि इस program की requirement क्या है।
Program Statement (Problem की पूरी समझ)
इस Gross Salary Calculation Program in C में हमें निम्नलिखित काम करना है:
- User से Basic Salary input लेना है।
- अगर Basic Salary 0 या उससे कम हो, तो “Invalid Basic Salary” message show करना है।
- अगर Salary valid है, तो नीचे दिए गए table के अनुसार HRA (House Rent Allowance) और DA (Dearness Allowance) calculate करना है।
| Basic Salary Range | HRA % | DA % |
|---|---|---|
| ₹0 – ₹10,000 | 20% | 80% |
| ₹10,001 – ₹20,000 | 25% | 90% |
| ₹20,000 से अधिक | 30% | 95% |
- अंत में Gross Salary = Basic + HRA + DA calculate करके display करना है।
यह table हमारे program की backbone है। हर salary range के लिए अलग-अलग percentage apply होती है, इसलिए हमें if-else if-else chain का use करना पड़ेगा।
Program Code — Gross Salary Calculator in C
#include <stdio.h>
int main()
{
float gsalary, basicSalary, hra, da;
printf("Enter Basic Salary: ");
scanf("%f", &basicSalary);
// Check whether the basic salary is valid
if (basicSalary <= 0)
{
printf("\nInvalid Basic Salary.\n");
}
else
{
// Calculate HRA and DA based on the basic salary
if (basicSalary <= 10000)
{
hra = basicSalary * 0.20f;
da = basicSalary * 0.80f;
}
else if (basicSalary <= 20000)
{
hra = basicSalary * 0.25f;
da = basicSalary * 0.90f;
}
else
{
hra = basicSalary * 0.30f;
da = basicSalary * 0.95f;
}
// Calculate gross salary
gsalary = basicSalary + hra + da;
printf("\nGross Salary: %.2f\n", gsalary);
}
return 0;
}
ह code clean, readable और beginners के लिए बिल्कुल सही है। अब इसे line-by-line समझते हैं।
Program Output — Expected Results
Case 1: Valid Salary Input
Enter Basic Salary: 10000
Gross Salary: 20000.00
Calculation verify करें:
HRA = 10000 × 0.20 = 2000, DA = 10000 × 0.80 = 8000, Gross = 10000 + 2000 + 8000 = 20000.00 ✔
Case 2: Invalid Salary Input
Enter Basic Salary: -5000
Invalid Basic salary
Case 3: Mid-Range Salary (₹15,000)
Enter Basic Salary: 15000
Gross Salary: 37500.00
Calculation verify करें:
HRA = 15000 × 0.25 = 3750, DA = 15000 × 0.90 = 13500, Gross = 15000 + 3750 + 13500 = 32250.00
Note: खुद हर case को manually calculate करके check करने की habit डालें — यह debugging skill बनाता है।
Code Explanation — Step-by-Step
यह section इस Gross Salary Calculation Program in C की सबसे important part है। हर step को ध्यान से पढ़ें।
Step 1: Header File Include करना
#include <stdio.h>
stdio.h standard input-output library है। इसके बिना printf() और scanf() functions काम नहीं करते। यह हर C program की पहली line होती है।
Step 2: Variables Declare करना
float gsalary, Bsalary, hra, da;
हमने float data type use किया है क्योंकि salary में decimal values आ सकती हैं (जैसे ₹10,500.50)। int use करने पर decimal part cut हो जाते हैं।
| Variable | काम |
|---|---|
| Bsalary | User की Basic Salary store करता है |
| hra | House Rent Allowance की value |
| da | Dearness Allowance की value |
| gsalary | Final Gross Salary |
Step 3: User से Input लेना
printf(“Enter Basic Salary: “);
scanf(“%f”, &Bsalary);
- printf() screen पर message print करता है।
- scanf() user की input को variable में store करता है।
- %f format specifier float value के लिए है।
- &Bsalary का मतलब है — variable का memory address pass करना ताकि scanf वहाँ value store कर सके।
Step 4: Invalid Salary Validate करना
if (Bsalary <= 0)
{
printf("\nInvalid Basic salary");
}यह input validation है। कोई भी professional program user की input को पहले check करता है। 0 या negative salary real world में possible नहीं है, इसलिए error message दिखाकर program रुक जाता है।
Step 5: Salary Range के आधार पर HRA और DA Calculate करना
यही इस program का main logic है:
if (Bsalary <= 10000)
{
hra = Bsalary * 0.2; // HRA = 20%
da = Bsalary * 0.8; // DA = 80%
}
else if (Bsalary <= 20000)
{
hra = Bsalary * 0.25; // HRA = 25%
da = Bsalary * 0.9; // DA = 90%
}
else
{
hra = Bsalary * 0.3; // HRA = 30%
da = Bsalary * 0.95; // DA = 95%
}Percentage को decimal में कैसे लिखें:
- 20% = 0.20, 25% = 0.25, 30% = 0.30
- 80% = 0.80, 90% = 0.90, 95% = 0.95
यह एक बेहद common beginner mistake है — 20/100 लिखने की बजाय सीधे 0.2 लिखें, वरना integer division की वजह से result 0 आ सकता है।
Step 6: Gross Salary का Formula Apply करना
gsalary = Bsalary + hra + da;
Gross Salary Formula:
Gross Salary = Basic Salary + HRA + DA
यह formula real-world payroll calculation में भी use होता है। HRA और DA के अलावा actual payroll में Medical Allowance, Travel Allowance आदि भी जोड़े जाते हैं, लेकिन basic exercise के लिए यह formula sufficient है।
Step 7: Output Print करना
printf(“\nGross Salary: %.2f”, gsalary);
- \n → नई line पर output print करता है।
- %.2f → float value को 2 decimal places तक दिखाता है, जो financial outputs के लिए professional standard है।
Common Mistakes जो Beginners करते हैं
इस Gross Salary Calculation Program in C को लिखते वक्त इन गलतियों से बचें:
- int की जगह float न लिखना — Decimal values miss हो जाती हैं।
- scanf में & भूल जाना — Program crash हो सकता है।
- Percentage को 20/100 लिखना — Integer division से result 0 आएगा; हमेशा 0.2 लिखें।
- else if की जगह दो अलग if लगाना — Logic गड़बड़ा जाएगा।
- Invalid input check न करना — Program unexpected results दे सकता है।
Program को और Better कैसे बनाएं (Bonus Tips)
अगर आप इस program को अगले level पर ले जाना चाहते हैं, तो इन improvements को try करें:
- Multiple employees के लिए loop add करें — for या while loop use करके एक साथ कई employees की salary calculate करें।
- Net Salary भी calculate करें — Net Salary = Gross Salary – Tax – PF (Provident Fund)।
- Output file में save करें — File handling सीखकर results को .txt file में store करें।
- Function बनाएं — HRA और DA calculate करने के लिए अलग-अलग functions define करें — यह code को reusable और clean बनाता है।
Conclusion ( निष्कर्ष )
Gross Salary Calculation Program in C एक ऐसा program है जो beginner से intermediate level तक हर C student को जरूर बनाना चाहिए। इस एक program में आपने float variables, user input, if-else if-else conditional logic, percentage calculation, और formatted output — यानी C programming के पाँच core concepts एक साथ सीखे। यह सिर्फ एक classroom exercise नहीं है; यही logic real-world payroll systems की नींव है।
अब आपकी बारी है! इस code को अपने compiler में type करें (copy-paste नहीं), खुद run करें, और ऊपर बताए गए improvements — जैसे loop add करना या Net Salary calculate करना — को try करें। जितना ज्यादा आप खुद experiment करेंगे, उतनी जल्दी आपकी C programming strong होगी। अगर कोई doubt हो, तो नीचे comment करें — हम जरूर help करेंगे!
