118k views
2 votes
Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with every purchase of 8 or more books. It offers regular customers 1 free book with every purchase of 7 or more books, and offers 2 free books with every purchase of 12 or more books. Write a statement that assigns freeBooks the appropriate value based on the values of the bool variable isPremiumCustomer and the int variable nbooksPurchased.

User Dube
by
6.4k points

1 Answer

4 votes

Answer:

The if else statements for the given scenario is shown below.

if ( isPremiumCustomer == true && nbooksPurchased >= 8 )

freeBooks = 2;

else if ( isPremiumCustomer == true && nbooksPurchased >= 5 && nbooksPurchased < 8 )

freeBooks = 1;

else if ( isPremiumCustomer == false && nbooksPurchased >= 12 )

freeBooks = 2;

else if ( isPremiumCustomer == false && nbooksPurchased >= 7 && nbooksPurchased < 12 )

freeBooks = 1;

Step-by-step explanation:

The number of free books to be given is determined by multiple if else statements based on the values of Boolean variable and number of books purchased.

The c++ program to implement the above statements is given below.

#include <iostream>

using namespace std;

int main() {

int nbooksPurchased = 7, freeBooks;

bool isPremiumCustomer = false;

// determines the value of freeBooks

if( isPremiumCustomer == true && nbooksPurchased >= 8 )

freeBooks = 2;

else if( isPremiumCustomer == true && nbooksPurchased >= 5 && nbooksPurchased < 8 )

freeBooks = 1;

else if( isPremiumCustomer == false && nbooksPurchased >= 12 )

freeBooks = 2;

else if( isPremiumCustomer == false && nbooksPurchased >= 7 && nbooksPurchased < 12 )

freeBooks = 1;

cout << " Premium customer " << " \t \t " << isPremiumCustomer << endl;

cout << " Number of free books " << " \t " << freeBooks << endl;

return 0;

}

OUTPUT

Premium customer 0

Number of free books 1

Initially, the Boolean variable and integer variables are declared and initialized.

The variables are initialized inside the program. No user input is taken.

The value of 1 for premium customer represents true.

For regular customer, 0 will be displayed for the premium customer statement.

The if statement consists of multiple conditions inside one pair of braces. The compact form of if else is used as only the statement is needed per the question.

User Oisin Lavery
by
5.1k points