Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents" on a line by itself. So, if the value of price was 4321, your code would print "43 dollars and 21 cents". If the value was 501 it would print "5 dollars and 1 cents". If the value was 99 your code would print "0 dollars and 99 cents".

Respuesta :

Answer:

The c++ statement to show price in dollars and cents is given.

if ( price  < 100 )

   {

       y = price;

   }

   else  

   {

       x = price/100;

       y = price%100;

   }

Explanation:

The program does not take any user input as it is not specified in the question.

All the three variables used are integer variables. The variables to store dollars and cents are initialized to 0.

The integer variable price is initialized inside the program.

The value of price is decomposed in dollars and cents as per the logic shown below.

if ( price  < 100 )

   {

       y = price;

   }

   else  

   {

       x = price/100;

       y = price%100;

   }    

If value of price is below 100, the value of price is taken as cents.

If value of price is 100 or above, cents are separated by taking modulo of price, and the remaining value is taken as dollars.

This program can be tested by taking different values for the variable price.

The c++ program for the above is as follows.

#include <iostream>

using namespace std;

int main() {    

// x shows dollars and y shows cents and are initialized to 0

// price can be initialized to any value for testing

   int x = 0, y = 0, price = 1234;

   cout << " This program displays the price in dollars and cents. " << endl;    

   if ( price  < 100 )

   {

       y = price;

   }

   else  

   {

// dollar is obtained by taking numbers before decimal

// cents are obtained by taking remainder on division by 100

       x = price/100;

       y = price%100;

   }    

   cout << x << " dollars and " << y << " cents " << endl;    

   return 0;

}

OUTPUT

This program displays the price in dollars and cents.

12 dollars and 34 cents