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

前端 未结 15 2156
遇见更好的自我
遇见更好的自我 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:43

    Here's my implementation: Simple and short

    #include 
    #include 
    using namespace std;
    
    
    int max_of_five(int a, int b, int c, int d,int e){
        int large= max(max(a, b), max(c,d));
        return max(large,e);
    
    }
    
    int min_of_five(int a,int b,int c, int d,int e){
        int small=min(min(a,b),min(c,d));
        return min(small,e);
    }
    
    
    int main() {
        int a, b, c, d,e;
    
        scanf("%d %d %d %d %d", &a, &b, &c, &d,&e);
        int ans = max_of_five(a, b, c, d, e);
        int ans1=min_of_five(a,b,c,d,e);
        printf("Max:\t%d\n", ans);
        printf("Min:\t%d", ans1);
    
        return 0;
    }
    

提交回复
热议问题