64,558 views
5 votes
5 votes
How can I use the max and min algorithm through a while loop in c++

User Sgupta
by
2.7k points

1 Answer

10 votes
10 votes

#include <algorithm> // For the max and min algorithms

#include <iostream> // For input and output streams

int main() {

// Initialize the max and min values

int max = std::numeric_limits<int>::min();

int min = std::numeric_limits<int>::max();

// Keep asking the user for numbers until they enter -1

int input = 0;

while (input != -1) {

std::cout << "Enter a number (-1 to stop): ";

std::cin >> input;

// Update the max and min values using the max and min algorithms

max = std::max(max, input);

min = std::min(min, input);

}

// Print the final max and min values

std::cout << "Max: " << max << std::endl;

std::cout << "Min: " << min << std::endl;

return 0;

}

User TwoThumbSticks
by
3.0k points