# Thread: largest and smallest number

1. ## largest and smallest number

This program is running good, the problem is that is suppose to print the smallest and largest, but sometimes it just prints the largest and sometimes it prints the smallest. I want both of them to be printed at the end. I want to do this program with if and if else... can one help me.

Fell free to give me your opinions and what my mistakes are. Thank you Very Much

Code:
```/*  Write a program that reads in four integers and determines and
prints the largest and the smallest integeers in the group.
*/
#include <stdio.h>

int main()
{

int a,b,c,d,e;

printf( "Enter number for 'ONE':\n" );
scanf( "%d", &a );
printf( "Enter number for 'TWO':\n" );
scanf( "%d", &b );
printf( "Enter number for 'Three':\n" );
scanf( "%d", &c );
printf( "Enter number for 'Four':\n" );
scanf( "%d", &d );
printf( "Enter number for 'Five':\n" );
scanf( "%d", &e );

if (a > b && a > c && a > d && a > e)
printf( "%d is the largest number\n", a);
else if(a < b && a < c && a < d && a < e)
printf( "%d is the smallest number\n", a);

else if(b > a && b > c && b > d && b > e)
printf( "%d is the largest number\n", b);
else if(b < a && b < c && b < d && b < e)
printf( "%d is the smallest number\n", b);

else if(c > a && c > b && c > d && c > e)
printf( "%d is the largest number\n", c);
else if(c < a && c < b && c < d && c < e)
printf( "%d is the smallest number\n", c);

else if(d > a && d > b && d > c && d > e)
printf( "%d is the largest number\n", d);
else if(d < a && d < b && d < c && d < e)
printf( "%d is the smallest number\n", d);

else if(e > a && e > b && e > c && e > d)
printf( "%d is the largest number\n", e);
else if(e < a && e < b && e < c && e < d)
printf( "%d is the smallest number\n", e);

return 0;
}```

2. In your program, Change the else if's to if's -

if ( a largest conditions )
else if ( a smallest conditions )

if ( b largest conditions )
else if ( b smallest conditions )

etc

EDIT - Also - "Write a program that reads in four integers [...]". You're reading in five integers

3. Use two additional vars for min and max. Set min and max to the first number you read in. Then, compare the min and max to the rest of the values.

EDIT: And, by the way, search the forums. . . this has been asked quite a few times.

4. ## ok, i got it!

Thank you twomers and Kennedy. As Twomers said what i had to do is change the code to this and it works:

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

int a,b,c,d,e;

printf( "Enter number for 'ONE':\n" );
scanf( "%d", &a );
printf( "Enter number for 'TWO':\n" );
scanf( "%d", &b );
printf( "Enter number for 'Three':\n" );
scanf( "%d", &c );
printf( "Enter number for 'Four':\n" );
scanf( "%d", &d );
printf( "Enter number for 'Five':\n" );
scanf( "%d", &e );

if (a > b && a > c && a > d && a > e)
printf( "%d is the largest number\n", a);
else if(a < b && a < c && a < d && a < e)
printf( "%d is the smallest number\n", a);

if(b > a && b > c && b > d && b > e)
printf( "%d is the largest number\n", b);
else if(b < a && b < c && b < d && b < e)
printf( "%d is the smallest number\n", b);

if(c > a && c > b && c > d && c > e)
printf( "%d is the largest number\n", c);
else if(c < a && c < b && c < d && c < e)
printf( "%d is the smallest number\n", c);

if(d > a && d > b && d > c && d > e)
printf( "%d is the largest number\n", d);
else if(d < a && d < b && d < c && d < e)
printf( "%d is the smallest number\n", d);

if(e > a && e > b && e > c && e > d)
printf( "%d is the largest number\n", e);
else if(e < a && e < b && e < c && e < d)
printf( "%d is the smallest number\n", e);

return 0;
}```

5. Just sort an array.
Code:
```#include <stdio.h>

void bubblesort ( int array[], int n )
{
int k, j, temp, flag;
for (k = n - 1; k > 0; --k)
{
flag = 1;
for (j = 0; k > j; ++j)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j+1];
array[j + 1] = temp;
flag = 0;
}
}
if (flag)  break;
}
}

int main ( void )
{
int numbers[] = { 3, 45, 31, 13, };
bubblesort(numbers, sizeof numbers  / sizeof numbers[0]);
printf("largest number = %d, smallest number = %d\n", numbers[3],
numbers[0]);
return 0;
}```
\$ ./a
largest number = 45, smallest number = 3

6. Here is a version which only uses if-else statements and 3 variables:

Code:
```#include <stdio.h>

int main(void) {
int num, min, max;
puts("Enter number:");
scanf("%d",&num);
min=max=num;
puts("Enter number:");
scanf("%d",&num);
if(num<=min)
min=num;
else max=num;
puts("Enter number:");
scanf("%d",&num);
if(num<=min)
min=num;
else max=num;
puts("Enter number:");
scanf("%d",&num);
if(num<=min)
min=num;
else max=num;
printf("The Largest is: %d. Smallest is: %d",max,min);
return 0;
}```
hope this helps...

7. Code:
```/*  Write a program that reads in four integers and determines and
prints the largest and the smallest integeers in the group.
*/
#include <stdio.h>
#include <limits.h>

#define MAX_INTS 4

int main()
{
int min = INT_MAX;
int max = INT_MIN;
int a[ MAX_INTS ];
int i;

for (i = 0; i < MAX_INTS; i++)
{
printf( "Enter number %d of %d : ", (i + 1), MAX_INTS);
scanf( "%d", &a[i] );

if ( a[i] < min) min = a[i];
if ( a[i] > max) max = a[i];
}

printf("%d is the smallest number\n", min);
printf("%d is the largest number\n", max);

return 0;
}```

8. This can also be done without an array.
Code:
```/* ... */
int value; /* instead of an array */
/* ... */
for ( i = 0; i < MAX_INTS; i++ )
{
/* prompt and user input into value */
if ( value < min )
{
min = value;
}
if ( value > max )
{
max = value;
}
}
printf("min = %d, max = %d\n", min, max);
/* ... */```

9. ## Improving the min & max

Thank you guys for your support, I have found that the program i have is not efficient and it has some bugs, because when i enter numbers equal numbers example: num1= 44, num2 =44, num3= 44, num4= 44. It wont print nothing.
The best way to do it with out doing an array is how Dave_Sinkula posted. I have modified the code, but i have a problem it dosent print the minimun number. It just prints a crazy number but the maximum number works fine.

can some one point out where is the error?

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

int i,MAX_INTS=4,value,min,max; /* instead of an array */

for ( i = 0; i < MAX_INTS; i++ )
{
printf("\nEnter a number: ");   /* prompt and user input into value */
scanf("%d",&value);

if ( value < min )
{
min = value;
}
if ( value > max )
{
max = value;
}
}
printf("min = %d, max = %d\n", min, max);
return 0;
}```
Thank you guys

10. You're using min and max uninitialized. You should really learn to pass at least -Wall to the compiler to catch these things. The header file "limits.h" defines two constants, INT_MIN and INT_MAX, that are useful in situations such as this.

Code:
``` 1 #include <stdio.h>
2 #include <limits.h>
3
4 int main()
5 {
6     int i,
7         value,
8         min = INT_MAX,
9         max = INT_MIN;
10
11     const int MAX_INTS = 4;
12
13     for ( i = 0; i < MAX_INTS; i++ )
14     {
15         printf("\nEnter a number: ");   /* prompt and user input into value */
16         scanf("%d",&value);
17
18         if ( value < min )
19         {
20             min = value;
21         }
22         if ( value > max )
23         {
24             max = value;
25         }
26     }
27     printf("min = %d, max = %d\n", min, max);
28     return 0;
29 }
30 ```

11. ## It works, Thanks

Thanks zx-1 i got it! I learn something new about the #include <limits.h>

I got it working and the code is really short compare to my first code. Thanks

12. All zx-1 did was mimic Skeane and put in pointless line numbers. If you actually read the replies people type to you it'll help you in the long run. Oh, and it saves us all time having to tell you again what you've already been told.

Quzah.