Seeing the solution to this online on Stackoverflow https://stackoverflow.com/a/19200031/3185410 I tried to come up with another solution by setting max to -infinity and min to +infinity. The code here by @haccks works pretty fine.
#include <stdio.h>
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 ("\n%d %d", max, min);
return 0;
}
Here's the one I came up with:
#include <stdio.h>
#include <limits.h>
void main()
{
int max,min,num,i;
min=INT_MAX;
max=INT_MIN;
for (i=0;i<=3;)
{
i++;
printf("Enter number %d : ",i);
scanf("%d",&num);
if (num>max) max=num;
else if (num<min) min=num;
}
printf("max is %d and min is %d",max,min);
}
What am I doing wrong with that?