how to find min of two numbers in c++
To find the minimum of two numbers in C++, you can use the min
function from the <algorithm>
library. Here are the steps to do that:
Include the
<algorithm>
header at the beginning of your code to access themin
function.Declare two variables to hold the numbers you want to compare. Let's call them
num1
andnum2
.Use the
min
function to find the minimum ofnum1
andnum2
. Themin
function takes two arguments and returns the smaller of the two.
cpp
int minimum = min(num1, num2);
In this example, the minimum
variable will hold the value of the smaller number.
- You can then use the
minimum
variable in your code as needed.
Here is an example code snippet that demonstrates finding the minimum of two numbers:
#include <iostream>
#include <algorithm>
int main() {
int num1 = 10;
int num2 = 5;
int minimum = min(num1, num2);
std::cout << "The minimum of " << num1 << " and " << num2 << " is: " << minimum << std::endl;
return 0;
}
In this example, the output will be:
The minimum of 10 and 5 is: 5
This code snippet demonstrates how to use the min
function to find the minimum of two numbers in C++.