Using for statement to find the greatest of four given integers?

Solution 1:

In max_of_four:

while (i >= 3) is never true because you start with i being 0, and 0 is not greater than or equal to 3. Perhaps you meant while (i <= 3), but you would normally write this loop using for rather than while:

for (int i = 0; i < 4; i++)
    if (greatest <= num[i]) greatest = num[i];

Solution 2:

The problem is with the while loop condition. The condition should have been while(i<=3).

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

    int greatest,i = 0;
    int num[4] = {a, b, c, d};
    greatest = num[0];
    while(i <= 3) {
        if(greatest <= num[i]) {
            greatest = num[i];
        }
        i++;
    }
    return greatest;
}