codescracker
c++

C++ Program to Calculate Shipping Charge as per the Weight of Parcel



« C++ Tutorial C++ Example »

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

This type of program may used on an E-commerce platform like Flipkart, Amazon etc. But this is not compulsory, they calculate the price not only based on the Parcel's weight. They have their own criteria. Forget that, let's create a program in C++ to find and print the shipping charge based on parcel's weight entered by user at run-time.

The criteria to calculate shipping charge based on parcel's weight is:

Program to Calculate Shipping Charge based on Weight

The question is, write a program in C++ that calculate and print the charge that has to be paid to ship a parcel. The charge must be calculated based on the weight of 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 above C++ program on calculating shipping charge based on Parcel's weight:

c++ program calculate shipping charge

Now type the weight of parcel say 3.4 and hit ENTER key to find the shipping charge based on its weight like 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, therefore 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++ Online Test


« C++ Tutorial C++ Example »



© Copyright 2021. All Rights Reserved.

CodesCracker