81.9k views
3 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 set of nested if/else statements that assigns freeBooks the appropriate value based on the values of the bool variable isPremiumCustomer and the int variable nbooksPurchased.

User Bboy
by
5.9k points

2 Answers

4 votes

Answer:

if (isPremiumCustomer)

{

if (nbooksPurchased >= 8)

freeBooks = 2;

else if (nbooksPurchased >= 5)

freeBooks = 1;

else

freeBooks = 0;

}

else

{

if (nbooksPurchased >= 12)

freeBooks = 2;

else if (nbooksPurchased >= 7)

freeBooks = 1;

else

freeBooks = 0;

}

Step-by-step explanation:

User Dan Paradox
by
5.8k points
6 votes

Answer:

Following are the if/else statement that are given below

if(nbooksPurchased > 4) //book purchase greater then 4

{

if(isPremiumCustomer) // check condition

{

freeBooks = 1; // free book is 1

if(nbooksPurchased > 7) // book purchase is 8

{

freeBooks = 2; // two free books

}

}

else

{

freeBooks = 0; // free books is 2

}

if(nbooksPurchased > 6) // book purchase is greater then 6

{

freeBooks = 1; // free books is 1

}

if(nbooksPurchased > 11) // book purchase is 12

{

freeBooks = 2; // free books is 2

}

}

}

else

{

freeBooks = 0; // free books is 0

}

Step-by-step explanation:

Following are the description of statement

  • As mention in the question the variable "nbooksPurchased." described the purchasing of books ,"freeBooks" is used to describe the free books and "isPremiumCustomer" is bool variable .
  • if nbooksPurchased >5 means the book is every purchase of the 5 it assign freeBooks = 1; .
  • If nbooksPurchased is greater then 7 then free books variable is assigned with the 2 otherwise free books is 0.
  • if nbooksPurchased is greater then 6 then it assign free books to 1.
  • if nbooksPurchased is greater then 11 then it assign the free books to 2 otherwise assign freebooks to 0.

User Stefan Krawczyk
by
6.0k points