Question: I made a C do while loop that displays the minimum and maximum. The loop ends when the user inputs a 0. 

My problem is when I enter a set of numbers all greater than 0, my minimum is zero.  
 For example when I enter 

```
19,16,8,12,9 

```

my **`max is 19`** ,but my **`min is 0`**.  
 I need the `min` to be the `min` of the set, so in this case is 8.  

I also have the problem with negative integers of the set 

```
-6,-4,-9,-12

```

The **`min is -12`**, but my **`max is 0`**. I want my **`max to be -4`** in this case.

How do I fix this. Is this because I have to enter 0 to end the program? Should I just try a while loop instead?

Any help is appreciated.

```
#include<stdio.h>
main()
{

    int L,S,X;

    do{
        printf("\ninput number:");
        scanf("%d",&X);
        if (X>L){
            L=X;
        }
        if (X<S){
            S=X;
        } 

    } while(X!=0);
    printf("\nmax %d, min %d",L,S);
    printf("\n");
    return 0;
}

```

Answer: 
Your variables `L` and `S` are uninitialized, so start with some unknown value. The min should start at INT\_MAX, the max should start at INT\_MIN.
You have to initialise the `L` and `S` variables with the minimum and maximum values, respectively. As `L` and `S` both are integers, set the value of `L` as `INT_MIN` and `S` as `INT_MAX` (These constants are present in `<limits.h>`). 

Hence, your code would be:

```
#include <stdio.h>
#include <limits.h>
main()
{
    int L = INT_MIN ,S = INT_MAX , X;
    printf("\nInput number:");
    scanf("%d",&X);
    while (X)
    {
        if (X>L){
            L=X;
        }
        if (X<S){
            S=X;
        } 
        printf("\nInput number:");
        scanf("%d",&X);
    } 
    printf("\nmax %d, min %d",L,S);
    printf("\n");
    return 0;
}

```

This code takes care of the `0` problem, too. Because, when you input the number, the next statement checks if number is `0` or not.