C Program to Calculate Discount on Bill Amount एक बेहद popular और practically उपयोगी programming exercise है, जिसे हर C language सीखने वाले beginner को जरूर समझना चाहिए। यह program न सिर्फ आपको conditional statements का सही उपयोग सिखाता है, बल्कि real-world billing system का basic logic भी समझाता है।
इस post में आप step-by-step सीखेंगे कि कैसे C language में if–else if–else condition का उपयोग करके bill amount पर discount calculate किया जाता है। साथ ही आप समझेंगे कि variables कैसे declare होते हैं, user से input कैसे लिया जाता है, और percentage-based discount की गणना कैसे होती है।
यह guide specially उन beginners के लिए लिखी गई है जो C programming में decision making और arithmetic operations सीख रहे हैं। अंत तक पढ़ने के बाद आप खुद से एक functional billing discount calculator बना सकेंगे।
Table of Contents
C Program to Calculate Discount on Bill Amount – क्यों सीखें?
आज के दौर में हर दुकान, mall, और online store पर bill amount के आधार पर discount दिया जाता है। जब आप C programming सीख रहे होते हैं, तो ऐसे real-life problems को code में implement करना आपकी logic-building को मजबूत बनाता है।
C Program to Calculate Discount on Bill Amount सीखने से आपको निम्नलिखित फायदे होते हैं:
- Conditional Logic समझ में आती है — if, else if, else का practical उपयोग
- Arithmetic Operations का अभ्यास होता है — percentage calculation
- Float Data Type का सही उपयोग समझ में आता है
- Real-world Programming Thinking develop होती है
Program Statement – Problem को समझें
इस program में हमें एक ऐसा C program बनाना है जो:
- User से Bill Amount input ले
- अगर Bill Amount ₹5000 या उससे अधिक हो → 20% discount दें
- अगर Bill Amount ₹3000 या उससे अधिक हो (लेकिन 5000 से कम) → 10% discount दें
- अगर Bill Amount ₹3000 से कम हो → कोई discount नहीं
- अंत में final payable amount print करें
यह logic real supermarkets और retail stores में भी इसी तरह काम करता है।
Program Code – C Program to Calculate Discount on Bill Amount
#include <stdio.h>
int main()
{
float billAmount, discountAmount, finalAmount;
printf("Enter Bill Amount: ");
scanf("%f", &billAmount);
// Calculate discount based on the bill amount
if (billAmount >= 5000)
{
discountAmount = (20.0f / 100) * billAmount;
}
else if (billAmount >= 3000)
{
discountAmount = (10.0f / 100) * billAmount;
}
else
{
discountAmount = 0;
}
// Calculate the final amount after discount
finalAmount = billAmount - discountAmount;
// Display discount details
if (discountAmount > 0)
{
printf("Congratulations! You got %.2f discount.\n", discountAmount);
printf("You should pay: %.2f\n", finalAmount);
}
else
{
printf("No discount.\n");
printf("You should pay: %.2f\n", finalAmount);
}
return 0;
}
Program Output – तीन Cases में देखें
Case 1: Bill Amount = ₹6000 (20% Discount)
Calculation: 6000 का 20% = 1200 → Final Amount = 6000 – 1200 = ₹4800
Case 2: Bill Amount = ₹3500 (10% Discount)
Calculation: 3500 का 10% = 350 → Final Amount = 3500 – 350 = ₹3150
Case 3: Bill Amount = ₹2000 (No Discount)
Calculation: 2000 < 3000 → कोई discount नहीं → Final Amount = ₹2000
Code Explanation – Line by Line समझें
1. Header File
#include <stdio.h>
यह standard input-output header file है। इसके बिना printf() और scanf() functions काम नहीं करते। हर C program की शुरुआत में यह लिखना जरूरी है।
2. Variables Declaration
float BillAmount, discountAmount, finalAmount;
तीन variables declare किए गए हैं:
- BillAmount → User द्वारा enter किया गया bill amount store होता है
- discountAmount → Calculate किया गया discount यहाँ store होता है
- finalAmount → Discount घटाने के बाद जो amount बचती है वो यहाँ आती है
float datatype इसलिए use किया गया है क्योंकि bill amounts में decimal values भी आ सकती हैं, जैसे ₹3500.50।
3. User से Input लेना
printf(“Enter Bill Amount : “);
scanf(“%f”, &BillAmount);
scanf(“%f”, &BillAmount) user द्वारा enter किया गया number BillAmount variable में store कर लेता है। %f format specifier float values के लिए use होता है।
4. Discount Logic – if–else if–else
यह program का सबसे महत्वपूर्ण हिस्सा है:
if (BillAmount >= 5000)
{
discountAmount = (20.0 / 100) * BillAmount;
}
else if (BillAmount >= 3000)
{
discountAmount = (10.0 / 100) * BillAmount;
}
else
{
discountAmount = 0;
}Condition की जाँच इस क्रम में होती है:
- पहले check होता है कि BillAmount ≥ 5000 है या नहीं → अगर हाँ, 20% discount
- अगर पहली condition false है, तो check होता है BillAmount ≥ 3000 → अगर हाँ, 10% discount
- दोनों false हों तो discount = 0
Important Note: 20.0 / 100 लिखा गया है न कि 20 / 100, क्योंकि अगर दोनों integer हों तो result 0 आ जाएगा। .0 लगाने से यह float division बन जाती है।
5. Final Amount Calculation
finalAmount = BillAmount – discountAmount;
Bill amount में से discount घटाने पर जो बचता है वही final payable amount होता है।
6. Output Display
printf(“Congratulation! You got %.2f Discount\n”, discountAmount);
printf(“You should Pay : %.2f\n”, finalAmount);
- %.2f का मतलब है output को 2 decimal places तक दिखाना, जैसे 1200.00
- अगर discount मिला तो congratulation message print होता है
- अगर नहीं मिला तो “No Discount” print होता है
Program को और बेहतर कैसे बनाएं?
अगर आप इस C Program to Calculate Discount on Bill Amount को और advanced बनाना चाहते हैं, तो निम्न improvements कर सकते हैं:
- Multiple discount slabs add करें (जैसे 30% for ₹10,000+)
- GST calculation भी add करें final amount पर
- Loop use करके multiple bills calculate करने की सुविधा दें
- Coupon Code feature add करें जो extra discount दे
- Output को formatted receipt की तरह print करें
if–else if–else vs Switch-Case – कब क्या use करें?
| Feature | if–else if–else | Switch-Case |
|---|---|---|
| Range-based conditions | ✅ Best choice | ❌ उपयुक्त नहीं |
| Exact value matching | ✅ काम करता है | ✅ Best choice |
| Floating point support | ✅ हाँ | ❌ नहीं |
| Readability | ✅ अच्छी | ✅ बेहतर (कई cases में) |
Discount calculation जैसे range-based problems के लिए हमेशा if–else if–else ही सबसे उपयुक्त होता है।
Conclusion ( निष्कर्ष )
C Program to Calculate Discount on Bill Amount एक बेहतरीन beginner-level exercise है जो आपको C language के तीन सबसे जरूरी concepts — variables, conditional statements, और arithmetic operations — एक साथ practice करने का मौका देती है। इस program को समझने के बाद आप न सिर्फ discount calculator बल्कि tax calculator, grade calculator, और salary calculator जैसे programs भी आसानी से बना सकते हैं। Real-world logic को code में translate करने की यह ability आपको एक बेहतर programmer बनाती है।
अगर आप C programming को seriously सीखना चाहते हैं, तो इस code को खुद type करके compile करें, अलग-अलग values से test करें, और ऊपर बताए गए improvements को implement करने की कोशिश करें। Practice ही programming में mastery की असली कुंजी है। अगर कोई doubt हो तो नीचे comment करें — हम जरूर help करेंगे!
