C Program To Find The Charges Of Shipping Company Following Their Discounts
This article will guide you through creating a C program to calculate shipping charges, incorporating various discount rates based on the package weight. You will learn to implement conditional logic (if-else if) to determine the final cost accurately.
Problem Statement
A shipping company determines its charges based on a base rate per kilogram. To incentivize larger shipments, they offer discounts based on the total weight of the package. The challenge is to write a program that takes the package weight as input and calculates the final shipping cost after applying the appropriate discount.
For example, a shipping company might have a base rate of $5.00 per kilogram with the following discount structure:
- No discount for packages up to 5 kg.
- 10% discount for packages over 5 kg and up to 10 kg.
- 15% discount for packages over 10 kg and up to 20 kg.
- 20% discount for packages over 20 kg.
Example
Consider a package weighing 7.5 kg with a base rate of $5.00/kg.
- Initial cost: 7.5 kg * $5.00/kg = $37.50
- Since 7.5 kg is over 5 kg and up to 10 kg, a 10% discount applies.
- Discount amount: $37.50 * 0.10 = $3.75
- Final cost: $37.50 - $3.75 = $33.75
Background & Knowledge Prerequisites
To understand and implement this program, readers should be familiar with:
- Basic C syntax: variable declaration, data types (e.g.,
floatordouble). - Input/Output operations: using
printffor output andscanffor input. - Conditional statements:
if,else if, andelsefor decision-making. - Arithmetic operators: addition, subtraction, multiplication, and division.
Use Cases or Case Studies
This type of program is valuable in several real-world scenarios:
- E-commerce Platforms: Automatically calculate shipping costs for customer orders based on total cart weight.
- Logistics Companies: Determine freight charges for clients, applying various tiers of discounts.
- Warehouse Management: Estimate shipping expenses for inventory transfers or outgoing shipments.
- Small Business Operations: Help small businesses quickly quote shipping costs for their products.
- Personal Budgeting Tools: For individuals sending packages, estimate the cost based on weight and potential discounts.
Solution Approaches
Calculating Shipping Charges with Conditional Discounts
This approach uses a series of if-else if statements to apply discounts based on the package's weight. It's a straightforward method for handling multiple conditional rules.
One-line summary
Implement conditional logic usingif-else if statements to apply varying discount percentages to the total shipping cost based on the package's weight.
Code example
// Shipping Charge Calculator with Discounts
#include <stdio.h>
int main() {
// Step 1: Declare variables for weight, base rate, total cost, and discount rate.
float weight_kg;
float base_rate_per_kg = 5.00; // Example base rate: $5.00 per kg
float total_cost;
float discount_rate = 0.0; // Initialize discount rate to 0%
// Step 2: Prompt user for package weight and read input.
printf("Enter the package weight in kilograms: ");
scanf("%f", &weight_kg);
// Step 3: Calculate the initial total cost before discount.
total_cost = weight_kg * base_rate_per_kg;
// Step 4: Apply discounts based on weight using if-else if statements.
if (weight_kg <= 5.0) {
discount_rate = 0.0; // No discount for 5 kg or less
} else if (weight_kg > 5.0 && weight_kg <= 10.0) {
discount_rate = 0.10; // 10% discount for >5 kg to 10 kg
} else if (weight_kg > 10.0 && weight_kg <= 20.0) {
discount_rate = 0.15; // 15% discount for >10 kg to 20 kg
} else { // weight_kg > 20.0
discount_rate = 0.20; // 20% discount for >20 kg
}
// Step 5: Calculate the final cost after applying the discount.
float discount_amount = total_cost * discount_rate;
float final_cost = total_cost - discount_amount;
// Step 6: Display the results to the user.
printf("\\n--- Shipping Details ---\\n");
printf("Package Weight: %.2f kg\\n", weight_kg);
printf("Base Rate: $%.2f per kg\\n", base_rate_per_kg);
printf("Initial Cost: $%.2f\\n", total_cost);
printf("Discount Applied: %.0f%%\\n", discount_rate * 100);
printf("Discount Amount: $%.2f\\n", discount_amount);
printf("Final Shipping Cost: $%.2f\\n", final_cost);
return 0;
}
Sample output
Enter the package weight in kilograms: 7.5
--- Shipping Details ---
Package Weight: 7.50 kg
Base Rate: $5.00 per kg
Initial Cost: $37.50
Discount Applied: 10%
Discount Amount: $3.75
Final Shipping Cost: $33.75
Stepwise explanation
- Variable Declaration:
-
weight_kg: Stores the weight input by the user. -
base_rate_per_kg: Holds the fixed rate for shipping per kilogram. -
total_cost: Stores the cost before any discounts. -
discount_rate: Stores the applicable discount percentage (as a decimal).
- Input:
- The program prompts the user to enter the package weight using
printf. -
scanfreads the floating-point value entered by the user and stores it inweight_kg.
- Initial Cost Calculation:
-
total_costis calculated by multiplyingweight_kgbybase_rate_per_kg.
- Discount Application (Conditional Logic):
- A series of
if-else if-elsestatements checks theweight_kgagainst predefined ranges. - If
weight_kgis 5 kg or less,discount_rateremains 0.0. - If
weight_kgis greater than 5 kg but up to 10 kg,discount_rateis set to 0.10 (10%). - If
weight_kgis greater than 10 kg but up to 20 kg,discount_rateis set to 0.15 (15%). - If
weight_kgis greater than 20 kg (theelsecase),discount_rateis set to 0.20 (20%).
- Final Cost Calculation:
-
discount_amountis calculated by multiplyingtotal_costby thediscount_rate. -
final_costis then determined by subtracting thediscount_amountfrom thetotal_cost.
- Output:
-
printfstatements display the package weight, base rate, initial cost, applied discount percentage, discount amount, and the final shipping cost, formatted to two decimal places for currency.
Conclusion
This article demonstrated how to create a C program that accurately calculates shipping charges by applying conditional discounts based on package weight. By using if-else if statements, the program efficiently handles various pricing tiers, providing a clear and precise final cost. This fundamental logic can be extended to more complex pricing models in real-world applications.
Summary
- Problem: Calculate shipping costs with weight-based discounts.
- Solution: Use
if-else ifstatements to apply different discount rates. - Key Steps:
- Input package weight.
- Calculate initial cost based on base rate.
- Determine discount rate using conditional logic (
if-else if). - Calculate discount amount and final cost.
- Display all relevant shipping details.
- Prerequisites: Basic C programming concepts (variables, I/O, conditionals).
- Benefits: Automates pricing, ensures accurate billing, and can be integrated into larger systems.