Functions in C

Sort by

recency

|

857 Discussions

|

  • + 0 comments
    int max_of_four(int a, int b, int c, int d)
    {
        int max = a;
        if(b > max)
            max = b;
        if (c > max)
            max = c;
        if (d > max)
            max = d;
        return max;
    }
    

    `

  • + 0 comments

    int max_of_four(int a, int b, int c, int d) { if ((a>b)&&(a>c)&&(a>d)) { return a; } else if ((b>a)&&(b>c)&&(b>d)) return b; else if ((c>a)&&(c>b)&&(c>d)) return c; else return d; }

  • + 0 comments

    Here is my version of solution using ternary operator

    int max_of_four(int a,int b,int c,int d) {

     int largest;
    
        largest= (a>=b && a>=c && a>=d)?a: (b>=c && b>=d)?b: (c>=d)?c:d;
    
        return largest;
    

    }

  • + 0 comments

    Here is HackerRank Functions in C problem solution

  • + 0 comments
    #include <stdio.h>
    #include<stdbool.h>
    
    int max_of_four(int a, int b, int c, int d) {
        
        while (true) {
            if ( (a > b && a > c) && (a > d) ) {
                return a;
            } else if ((b > a && b > c) && (b > d)) {
                return b;
            } else if ( (c > a && c > b) && (c > d)) {
                return c;
            } else if ((d > a && d > b) && (d > c)) {
                return d;
            }
        }
        return 0;
        
    }
    
    
    int main() {
        int a, b, c, d;
        scanf("%d %d %d %d", &a, &b, &c, &d);
        int ans = max_of_four(a, b, c, d);
        printf("%d", ans);
    		return 0;
    }    
        return 0;
    
    
    /*
    	I want to share my solution to this challenge.
    
    I used a while loop to compare which letter is greater than the rest.
    
    However, I would like to receive some suggestions to see if my solution is really valid or if it can be improved. Thank you very much.
    
    */
    }