Program to find largest and smallest among 5 numbers without using array

前端 未结 15 2157
遇见更好的自我
遇见更好的自我 2021-01-06 07:25

Yesterday I went for an interview where I have been asked to create a program to find largest and smallest among 5 numbers without using array.

I know how to create

15条回答
  •  天涯浪人
    2021-01-06 07:49

    You can do something like this:

    int min_num = INT_MAX;  //  2^31-1
    int max_num = INT_MIN;  // -2^31
    int input;
    while (!std::cin.eof()) {
        std::cin >> input;
        min_num = min(input, min_num);
        max_num = max(input, max_num);
    }
    cout << "min: " << min_num; 
    cout << "max: " << max_num;
    

    This reads numbers from standard input until eof (it does not care how many you have - 5 or 1,000,000).

提交回复
热议问题