C Program to Compute Charges for Sending Parcels as Per Their Weight

In this post, we will learn about how to create a program in C that will compute and print the charge (that has to be paid by the sender) to send a parcel as per its weight. That is, the program will ask the sender (or user) to enter the weight of the parcel and will calculate and print the charge for sending that parcel (of the given weight).

The program is designed in the following manner:
A post office charges parcel senders according to the weight of the parcel. For each parcel having a weight of 2 kg or less, the charge is 32.50, and for each extra kg, there is an additional charge of 10.50.

#include<stdio.h>
#include<conio.h>
int main()
{
    float weight, initCharge=32.50, perCharge=10.50, tempWeight, addCharge, totalCharge;
    printf("Enter weight of the parcel (in Kg): ");
    scanf("%f", &weight);
    printf("\n");
    if(weight<=2)
        printf("Charge = %0.2f", initCharge);
    else
    {
        tempWeight = weight-2;
        addCharge = tempWeight * perCharge;
        totalCharge = addCharge + initCharge;
        printf("Charge = %0.2f", totalCharge);
    }
    getch();
    return 0;
}

As the above program was written in the Code::Blocks IDE, here is the sample run after a successful build and run. This is the first snapshot:

c program calculate parcel charge

The user must now enter the weight of the parcel to determine the total charge that must be paid. Here is the second snapshot of the same sample run:

print parcel charge c program

If the weight of the parcel is less than or equal to 2, then there is only 32.50 that has to be paid by the sender. Let's take a look at another sample run:

c program print parcel charge

Let's take another sample run, as given below. This time the user has entered the parcel weight as 1.5 kg:

c program parcel charge with parcel weight

Below are some of the main steps used in the above program:

C Quiz


« Previous Program Next Program »