Advertisements
Advertisements
प्रश्न
Write a C++ program to find the smallest of five given integers using the min() function to return the smallest of five given integers.
कोड लेखन
Advertisements
उत्तर
#include <iostream>
#include <algorithm> // For min()
using namespace std;
int main()
{
int a, b, c, d, e;
cout << "Enter five integers: ";
cin >> a >> b >> c >> d >> e;
int smallest = min(min(min(min(a, b), c), d), e);
cout << "Smallest number is: " << smallest;
return 0;
}
- The
min()function (from<algorithm>) compares two values and returns the smaller one. - Since
min()works with two values at a time, we nest the function calls to compare all five numbers. - The final result is stored in the variable
smallest.
Example
Input: 10 25 3 40 18
Output: Smallest number is: 3
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
2024-2025 (July) Official Board Paper
