'Can't find the largest odd number

Write a program that examines three variables — x, y, and z — and prints the largest odd number among them. If none of them are odd, it should print a message to that effect.

This is my source code:

#include <stdio.h>

int main() {
    int x,y,z;
    x = 11;
    y = 15;
    z = 18;

    if (x > y && x > z && x % 2 != 0)
        printf("%d", x);
    else if (y > z && y > x && y % 2 != 0)
        printf("%d", y);
    else if (z > x && z > y && z % 2 != 0)
        printf("%d", z);
    else
        printf("error");

    return 0; 
}

The program is compiling and running but is giving the wrong answer. For the above it gives "error" as the output, but the greatest odd number is 15.

c


Solution 1:[1]

You are just printing the largest integer of the three if it is odd. For not to change you code so much and if you doesn't care about the others values, you can set the even variables to -INF. That is:

//INF can be whatever big even value you think that works fine (1e9), 0x3ffffffe, etc
#define INF 1e9
if (x % 2 == 0) x = -INF;
if (y % 2 == 0) y = -INF;
if (z % 2 == 0) z = -INF;

//here the rest of your code

Solution 2:[2]

In your example, y is the largest odd number, but it is not the largest number. z is larger, so "y>z" evaluates to false, and the path you want is not taken.

Solution 3:[3]

You may have noticed that your code is very repetitive. And the next exercise in the book is likely to be "OK, now do this for ten input numbers", which would be too tedious to contemplate, or "... for any number of input numbers", which just plain can't be done the way you're doing it.

So instead you should be thinking about a code structure like this:

int current_largest_odd_number = 0; /* note: zero is even */
for (;;) {
    int n = read_a_number();
    if (n == -1) break;  /* read_a_number returns -1 on EOF */
    if (is_even(n)) continue;
    if (n > current_largest_odd_number)
        current_largest_odd_number = n;
}
if (current_largest_odd_number > 0)
    printf("largest odd number: %d\n", current_largest_odd_number);

Solution 4:[4]

The code is not printing the largest odd number, but is printing the largest number if it happens to be odd.

You need to keep track of the largest odd number you've found and then print that.

For example:

int found = false, largest = 0;

if (x%2 != 0) {
    found = 1;
    largest = x;
}
if (y%2 != 0) {
    found = 1;
    if (y > largest) {
        largest = y;
    }
}
if (z%2 != 0) {
    found = 1;
    if (z > largest) {
        largest = z;
    }
}

if (found) {
    printf("%d", largest);
} else {
    printf("error");
}

Solution 5:[5]

Your code prints the largest number if and only if is is odd.

You could instead test each number in turn and update a pointer to the maximum odd value if any.

#include <stdio.h>

int main() {
    int x = 11;
    int y = 15;
    int z = 18;
    int *largest = NULL;

    if (x % 2 != 0) {
        largest = &x;
    }
    if (y % 2 != 0) {
        if (!largest || y > *largest) {
            largest = &y;
        }
    }
    if (z % 2 != 0) {
        if (!largest || z > *largest) {
            largest = &z;
        }
    }
    if (largest) {
        printf("%d\n", *largest);
    } else {
        printf("error\n");
    }
    return 0;
}

Here is an alternative approach with an indicator instead of a pointer:

#include <stdio.h>

int main() {
    int x = 11;
    int y = 15;
    int z = 18;
    int largest = 0;
    int found = 0;

    if (x % 2 != 0) {
        largest = x;
        found = 1;
    }
    if (y % 2 != 0) {
        if (!found || y > largest) {
            largest = y;
            found = 1;
        }
    }
    if (z % 2 != 0) {
        if (!found || z > largest) {
            largest = z;
            found = 1;
        }
    }
    if (found) {
        printf("%d\n", largest);
    } else {
        printf("error\n");
    }
    return 0;
}

Solution 6:[6]

My version examines all possible three integer numbers and prints the largest odd number (As an absolute beginner.)

x = int(input("number x = "))
y = int(input("number y = "))
z = int(input("number z = "))
/*Ask the user to input three variables*/
ans=0
if x%2==0 and y%2==0 and z%2==0:
    print("There are no odd numbers.")
else: 
if x<=y<=z and z%2!=0:
    ans=z
elif y%2!=0:
    ans=y
elif x%2!=0:
    ans=x
if x<=y>=z and y%2!=0:
    ans=y
elif x>=z and x%2!=0:
    ans=x
elif z%2!=0:
    ans=z
if x>=y<=z and x>=z and x%2!=0:
    ans=x
elif z%2!=0:
    ans=z
elif y%2!=0:
    ans=y
if x>=y>=z and x%2!=0:
    ans=x
elif y%2!=0:
    ans=y
elif z%2!=0:
    ans=z
print("The largest odd number is "+str(ans))
     

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 lambda
Solution 3 zwol
Solution 4
Solution 5 chqrlie
Solution 6 HugoWang