मराठी
महाराष्ट्र राज्य शिक्षण मंडळएचएससी विज्ञान (संगणक विज्ञान) इयत्ता १२ वी

Write a C++ program to find the smallest of five given integers using the min() function to return the smallest of five given integers. - Computer Science 1

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
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×