C programming problem to find largest number

I’m trying to make a function which help to find largest number.
But it’s not work properly.If i input gradually 2 5 6 8 then output is 8;That’s mean Right!
But if i input 2 8 6 5 then output is 5;That’s mean function is not work properly.
where i did mistake :frowning:
Can you help me please to figure that out
Here is my code-

#include <stdio.h>

int max_of_four(int a,int b, int c, int d)
    int largest;
    if(a >= b && a >= c && a >= d);
        largest = a;
    if(b >= a && b >= c && b >= d);
        largest = b;
    if(c >= a && c >= b && c >= d);
        largest = c;
    if(d >= a && d >= b && d >= c);
        largest = d;

    return largest;
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;

can’t u use the max ? to get the biggest number? like with min does to get the smallest?

sorry,i don’t get that! Can you please explain a bit kittykora?

like one of those that uses the words max and get to get the largest number our value


def max(arr):
    max_ = arr[0]
    for item in arr:
        if item > max_:
            max_ = item
    return max_ 


i don’t know allot about c but if it even a lil like python/js it should have this function :3 our somehting of this alike


Hi there,

In order to make your program work as you want you have to return largest every time when the IF condition is TRUE.

One of the reasons why your function is not working properly is that the && operator’s associativity is left to right. So when a is 2, b is 8, c is 6 and d is 5, this if condition:

    if(d >= a && d >= b && d >= c);
        largest = d;

returns true because of:

if (5 >= 2 && 5 >= 8 && 5 >= 6)

is interpreted like

if (true && false && false) // true && false is FALSE

is interpreted like:

if (false && false) // false && false is TRUE

then 5 is assigned to largest

1 Like

What if you wanted to find the max of 5 numbers? What about a thousand? You need to use the algorithm @KittyKora posted.


Thank you so much. I really understand the problem but how can i fix this ?

You should implement the following logic:

if (is a bigger than b?) {
  if (is a bigger than c?) {
    if (is a bigger than b?) {
      then a is the largest one
1 Like

I would be careful about chaining if statements together - you can write a lot more code than you need to. Big complex chains of if statements can be very hard to read, understand, and debug.

If I, as a human, look at a list and try to find the maximum, then I try to do something like @KittyKora describes. I read from left to right, keeping track of the largest number I’ve seen.

In code

int max = a;

if (b > max)
  max = b;
if (c > max)
  max = c;
if (d > max)
  max = d;

You can use this inside of your current function, but it will only work for 4 numbers. As others have mentioned, its good to generalize if its not too much effort to do so. If you rewrite this to use an array and a loop, like @KittyKora has done, then you can do this with much less code.

int lookForMax(int *arrayOfNumbers, int sizeOfArray) {
  int max = arrayOfNumbers[0];

  for (int i=1; i<sizeOfArray; i++) // Don't need to check array[0]! Why?
    // Code to check for largest between array[i] and max goes here

  return max;

You don’t always want to try to write as little code as possible, but if you have nested if statements, you should always ask yourself if you can get the job done easier!


Also, your syntax for your if statement is a little bit off for C:

if (condition) {
  code if true;
} else {
  code if false;

or, if you only have one line of code as a result of the if:

if (condition)
  code if true;
  code if false;

The semicolons go after the lines of code, not after the condition!

1 Like