C++ Program to find out the total price

C++Server Side ProgrammingProgramming

Suppose, we have gone to a shop to buy three items. We have to buy two items of each kind. We have to buy items of prices b and d, but there is a choice between buying items of prices a and c. We buy the lowest priced item between a and c. Find out the total of the items we have purchased, an extra amount of 2 is added to the total for tax purposes. Multiply it by two, and print the total price.

Problem Category

Various problems in programming can be solved through different techniques. To solve a problem, we have to devise an algorithm first, and to do that we have to study the particular problem in detail. A recursive approach can be used if there is a recurring appearance of the same problem over and over again; alternatively, we can use iterative structures also. Control statements such as if-else and switch cases can be used to control the flow of logic in the program. Efficient usage of variables and data structures provides an easier solution and a lightweight, low-memory-requiring program. We have to look at the existing programming techniques, such as Divide-and-conquer, Greedy Programming, Dynamic Programming, and find out if they can be used. This problem can be solved by some basic logic or a brute-force approach. Follow the following contents to understand the approach better.

So, if the input of our problem is like a = 10, b = 20, c = 15, d = 12, then the output will be 88.


To solve this, we will follow these steps −

e := (if a < c, then a, otherwise c)
print( 2 * (e + b + d + 2))


Let us see the following implementation to get better understanding −

using namespace std;
void solve(int a, int b, int c, int d) {
   int e = a < c ? a : c;
   cout<< 2 * (e + b + d + 2);
int main() {
   int a = 10, b = 20, c = 15, d = 12;
   solve(a, b, c, d);
   return 0;


10, 20, 15, 12


Updated on 07-Apr-2022 10:20:47