C++ program to calculate shipping charges as per the weight of the parcel

This article contains a program in C++ that is used to find and print the shipping charge based on the weight of the parcel that has to be sent.

This type of program may be used on an e-commerce platform like Amazon, Ebay, Walmart etc. However, this is not required; they calculate the price based on more than just the weight of the parcel. They have their own criteria. Forget that; let's create a program in C++ to find and print the shipping charge based on the parcel's weight entered by the user at run-time.

The following are the criteria for calculating the shipping charge based on the weight of the parcel:

Program to calculate shipping charges based on weight

The question is: write a program in C++ that calculates and prints the charge that has to be paid to ship a parcel. The charge must be calculated based on the weight of the parcel. Here is its answer:

#include<iostream>

using namespace std;
int main()
{
   float weight, basecharge=28.50, perkgcharge=4, temp, extracharge, charge;
   cout<<"Enter Parcel's Weight (in Kg): ";
   cin>>weight;
   if(weight<=2)
      cout<<"\nShipping Charge: "<<basecharge;
   else
   {
      temp = weight-2;
      extracharge = temp * perkgcharge;
      charge = extracharge + basecharge;
      cout<<"\nShipping Charge: "<<charge;
   }
   cout<<endl;
   return 0;
}

Here is the initial output produced by the above C++ program on calculating shipping charges based on a parcel's weight:

c++ program calculate shipping charge

Now type the weight of the parcel, say 3.4, and hit the ENTER key to find the shipping charge based on its weight, as shown in the snapshot given below:

c++ calculate shipping charge based on parcel weight

That is, since the weight of the parcel is 3.4, the shipping charge is calculated in this way:

weight          = 2 Kg + 1.4 Kg
Shipping charge = 28.5 + (1.4*4)
                = 28.5 + 5.6
                = 34.1

C++ Quiz


« Previous Program Next Program »