You see, I\'ve self-taught myself C++ (not completely, I\'m still procrastinating -_-). So, now I started university and they\'re teaching C and they made us do a program of
As per the OP's condition
But seeing that all we've covered in class for now are loops and decision statements. Is there a more elegant way of doing this? One which uses fewer
if
s?
Only one if
and one else if
statement and one for
loop can do this task. Simple and short!
#include
int main()
{
int num, max, min;
printf ("Enter four numbers: ");
scanf ("%d", &num);
max = min = num;
for (int i = 0; i < 3; i++)
{
scanf ("%d", &num);
if (max < num)
max = num;
else if (min > num)
min = num;
}
printf ("The smallest and largest of given four numbers are %d and %d respectively.\n", min, max);
return 0;
}