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.