Chapter 4 – #1: Minimum/Maximum – Tony Gaddis – Starting Out With C++

>_ Problem

Write a program that asks the user to enter two numbers. The program should use the conditional operator to determine which number is the smaller and which is the larger.

>_ Solution

#include <iostream>
using namespace std;

int main()
{
    // Variables
    int num1, 
        num2;

    // Ask the user to enter two numbers
    cout << endl;
    cout << "Enter Number 1: ";
    cin >> num1;
    cout << "Enter Number 2: ";
    cin >> num2;

    // Formatting
    cout << endl;

    // Use a decision structure to determine largest and smallest num
    if (num1 < num2)
    {
        cout << "Number 1 (" << num1 << ")";
        cout << " is smallest" << endl;

        cout << "Number 2 (" << num2 << ")";
        cout << " is largest"  << endl;
    }
    else
    {
        cout << "Number 1 (" << num1 << ")";
        cout << "is largest"  << endl;
        
        cout << "Number 2 (" << num2 << ")";
        cout << " is smallest" << endl;
    }

    // Formatting
    cout << endl;

    // Terminate
    return 0;
}

>_Get The Book

Tony Gaddis’ book “Starting Out with C++ from Control Structures to Objects”: 8th and 9th editions.

>_ Video Walk Through

This video includes a step by step process of how I solve programming challenge #1.

>_That’s it!

Thank you for taking interest in what I do! I hope it was helpful for you as much as it helped me along my journey in learning to code!

>_ Also Check Out:

Category: Starting Out With C++ | Chapter 4 | Programming Challenge Solutions

Leave a Reply