# Thread: Help with if statement program plz

1. ## Help with if statement program plz

Hi,
I'm having difficulty writing a c program that asks the user to input 4 integers and it find the smallest and largest integer. I can only use 4 "if" statements to complete the program. I know the program does not compute the smallest integer correctly, but how can i fix it ? This is what I have so far..

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

int main()
{
int num1, num2, num3, num4;
int smallest, largest;

printf("Enter four integers: ");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);

smallest = num1;

if (num1 >= num2)
if (num1 >= num3)
if (num1 >= num4)
largest = num1;
else
largest = num4;
else if (num4 >= num3)
largest = num4;
else
largest = num3;
else
largest = num2;

printf("Largest: %d\n", largest);
printf("Smallest: %d\n", smallest);

return 0;
}```

2. Have you encountered arrays yet? Do you really need to keep track of each number, or just the big and small? Have you used loops yet?
Code:
```smallest = big number
biggest = small number

repeat
if number < smallest
smallest = number
if number > biggest
biggest = number```
Set your repeat condition depending on how many times you want to repeat, or until you enter something that means 'quit looping'.

Quzah.

3. Ya, just set your biggest number to 0, loop through all numbers saying if this number is bigger, set it to your current number. Than you only need one if statement (two if you do the smallest number as well).

Code:
```num = 0;
for(i=0;i<4;i++)
{
if ( num[i] > num )
num =  num[i];
}```
And essentially do the opposite for the smallest number...

This will set num to the biggest number, because the only time it's overwritten is if a number is larger and vice versa. you will have to switch your implementation to an array.

4. We prefer not to give out solutions, especially in code, especially since Quzah just posted the pseudo code for that snippet you posted...
Leave some practice for the OP.