इस Program में आप सीखेंगे कि C language में if–else if–else condition का उपयोग करके discount कैसे calculate किया जाता है।
आप इस Post में सीखने वाले हैं:
- Conditional statements कैसे काम करती हैं
- Percentage के हिसाब से discount कैसे निकालते हैं
- Final payable amount कैसे calculate करते हैं
- Real-life billing system का basic logic
यह program खासकर beginners के लिए useful है जो C में decision making सीख रहे हैं।
Program Statement
एक ऐसा C program लिखिए जो user से Bill Amount input ले।
- अगर Bill Amount 5000 या उससे अधिक है तो 20% discount दें।
- अगर Bill Amount 3000 या उससे अधिक है तो 10% discount दें।
- अन्यथा कोई discount न दें।
अंत में final payable amount print करें।
Program Code
#include <stdio.h>
int main()
{
float BillAmount, discountAmount, finalAmount;
printf("Enter Bill Amount : ");
scanf("%f", &BillAmount);
if (BillAmount >= 5000)
{
discountAmount = (20.0 / 100) * BillAmount;
}
else if (BillAmount >= 3000)
{
discountAmount = (10.0 / 100) * BillAmount;
}
else
{
discountAmount = 0;
}
finalAmount = BillAmount - discountAmount;
if (discountAmount > 0)
{
printf("Congratulation! 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 :
Case 1:
Enter Bill Amount : 6000
Congratulation! You got 1200.00 Discount
You should Pay : 4800.00
Case 2:
Enter Bill Amount : 3500
Congratulation! You got 350.00 Discount
You should Pay : 3150.00
Case 3:
Enter Bill Amount : 2000
No Discount
You should Pay : 2000.00
Code Explanation
1️. Header File
#include <stdio.h>
यह standard input-output header file है, जिससे printf() और scanf() functions use कर सकते हैं।
2️. Variables Declaration
float BillAmount, discountAmount, finalAmount;
- BillAmount → User द्वारा enter गया Bill Amount को store करने के लिये
- discountAmount → Discount Amount को store करने के लिये
- finalAmount → Discount के बाद payable amount store करने के लिये
float datatype इसलिए use किया गया है ताकि decimal values handle किये जा सकें।
3️. Input लेना
scanf(“%f”, &BillAmount);
User से bill amount लेने के लिये है।
4️. Discount Logic (if–else if–else)
if (BillAmount >= 5000)
{
discountAmount = (20.0 / 100) * BillAmount;
}
अगर BillAmount 5000 या उससे ज्यादा है → 20% discount
else if (BillAmount >= 3000)
{
discountAmount = (10.0 / 100) * BillAmount;
}
अगर 3000 से ज्यादा लेकिन 5000 से कम है → 10% discount
else
3000 से कम होने पर → कोई discount नहीं
5️. Final Amount Calculation
finalAmount = BillAmount – discountAmount;
Total bill से discount घटाकर final payable amount निकाला गया है।
6️. Output Display
अगर discount मिला है तो congratulation message show होगा, नहीं तो “No Discount” print होगा।
%.2f का मतलब है output 2 decimal places तक दिखाना।
