C++ Practice Problem – Maximum Number Problem

Time Limit: 2s Memory Limit: 192MB

Problem Description

Input several integers, ending with -1. Output the maximum number among them.

Input Format

Several integers. (End input with -1)

Output Format

The maximum number among them.

Sample Input

1 2 5 7 8 6 1 -6 -1

Sample Output

8

Code

#include <iostream>#include <climits> // For INT_MINusing namespace std;
int main() {    int num;    int max_num = INT_MIN; // Initialize to the smallest integer
    while (true) {        cin >> num;
        // Check for end marker -1        if (num == -1) {            break;        }
        // Update maximum value        if (num > max_num) {            max_num = num;        }    }
    cout << max_num << endl;
    return 0;}

Output Result

C++ Practice Problem - Maximum Number ProblemCode Explanation:Header Files: <iostream> for input and output <climits> includes INT_MIN, representing the minimum integer valueInitialization: max_num = INT_MIN initializes the maximum value to the smallest integer This ensures that any input number will be larger than the initial valueLoop to Read Input: Use while (true) for an infinite loop Read one integer each time If -1 is read, use break to exit the loopUpdate Maximum Value: Each time a new number is read, compare it with the current maximum value If the new number is larger, update the maximum valueOutput Result: After the loop ends, output the found maximum value

Another Implementation Method (Using do-while Loop):

#include <iostream>#include <climits>using namespace std;
int main() {    int num;    int max_num = INT_MIN;
    do {        cin >> num;        if (num != -1 && num > max_num) {            max_num = num;        }    } while (num != -1);
    cout << max_num << endl;
    return 0;}

Third Method (Using the First Number for Initialization):

#include <iostream>using namespace std;
int main() {    int num;    int max_num;
    // First read the first number    cin >> num;    if (num == -1) {        cout << "No valid number input" << endl;        return 0;    }    max_num = num;
    // Continue reading the remaining numbers    while (cin >> num && num != -1) {        if (num > max_num) {            max_num = num;        }    }
    cout << max_num << endl;
    return 0;}

Extended Knowledge:INT_MIN: Is a constant defined in the C++ standard library, representing the minimum value of int type Typically -2147483648 (on 32-bit systems) Using INT_MIN for initialization ensures that any input number will be larger than itInput Handling Tips: Using while (cin >> num && num != -1) can check both if the input is successful and if the value is -1 This method can avoid entering an infinite loop when input errors occurTeaching Suggestions: The first method is the most recommended, with clear and robust logic Explain why the maximum value needs to be initialized to INT_MIN Emphasize handling edge cases (e.g., when all inputs are negative)Notes:If the first number input is -1, the program will output INT_MINChecks can be added to give a prompt if no valid numbers are inputThe problem guarantees at least one valid number (since it ends with -1)C++ Practice Problem - Maximum Number Problem

C++ Basic Tutorial Collection

C++ Practice Problem - Maximum Number ProblemC++ Basic Materials

1. C++ Output

2. C++ Variables

3. C++ Input

4. C++ Expressions

5. IF Statement

6. IF Applications

7. WHILE Loop Statements

8. FOR Loop Statements

9. Arrays

10. One-Dimensional Arrays

11. Two-Dimensional Arrays

12. C++ Functions

13. C++ File Operations – Writing Files

If you find it interesting, just click on me

C++ Practice Problem - Maximum Number ProblemC++ Practice Problem - Maximum Number ProblemC++ Practice Problem - Maximum Number ProblemC++ Practice Problem - Maximum Number Problem

Leave a Comment