Solutions

4. Variables

Exercise 2:

#include <iostream>
using namespace std; 
int main()
{
    cout << "Type in the price: " << endl;         
    double price=0;
    cin >> price;
    double discount = 0.1*price;
    cout << "After a 10% discount: " << price - discount << endl;
}

Exercise 3:

#include <iostream>
using namespace std;

int main()
{
    cout << "Type in the price before discount: " << endl;         
    double priceA=0;
    cin >> priceA;
    
    cout << "Type in the price after discount: " << endl;         
    double priceB=0;
    cin >> priceB;
    
    double discount = 1 - priceB/priceA;    
    cout << "The discount rate is: " << discount*100 << "%" << endl;
}

Exercise 4:

#include <iostream>
using namespace std;

int main()
{
    cout << "Type in the length of a movie in hours and minutes." 
         << endl;
    cout << "You can put each number in its own line or separate" 
         << endl;
    cout << "the numbers by a space." << endl;
    double hours=0;
    cin >> hours;        
    double minutes=0;
    cin >> minutes;
                
    double timeInMinutes = hours*60 + minutes;    
    cout << "The length is " << timeInMinutes << " minutes." << endl;
}

Exercise 5:

#include <iostream>
using namespace std;

int main()
{
    cout << "Enter the length of a movie in hours, " 
         << "minutes and seconds:" << endl;
    double hours=0;
    cin >> hours;        
    double minutes=0;
    cin >> minutes;
    double seconds=0;
    cin >> seconds;
                
    double timeInMinutes = hours*60 + minutes;    
    double timeInSeconds = timeInMinutes*60 + seconds;    
    cout << "The length is " << timeInSeconds << " seconds." << endl;
}

Back to exercises