How to Find the lowest value of two numbers in C++ ?


There are multiple ways to find the lowest value of two numbers in C++. Here are some of them:

  • Using if-else statement:
#include <iostream>
using namespace std;

int main() {
   int num1 = 10, num2 = 20;
   int lowest;

   if (num1 < num2) {
      lowest = num1;
   } else {
      lowest = num2;
   }

   cout << "The lowest value is: " << lowest << endl;
   return 0;
}

Output: The lowest value is: 10

  • Using ternary operator:
#include <iostream>
using namespace std;

int main() {
   int num1 = 10, num2 = 20;
   int lowest = (num1 < num2) ? num1 : num2;

   cout << "The lowest value is: " << lowest << endl;
   return 0;
}

Output: The lowest value is: 10

  • Using the min() function from the <algorithm> library:
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
   int num1 = 10, num2 = 20;
   int lowest = min(num1, num2);

   cout << "The lowest value is: " << lowest << endl;
   return 0;
}

Output: The lowest value is: 10



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.