Problem : Read three integers and sort them in ascending order. After, print these values in ascending order, a blank line and then the values in the sequence as they were inputted.
input: 7 21 -14
output: -14
7
21
7
21
-14
I have solved this problem in this way:
#include <stdio.h>
int main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if (a < b && a < c)
{
//first smallest number identified
printf("%d\n",a);
if (b < c)
{
//second and third smallest number identified
printf("%d\n",b);
printf("%d\n",c);
printf("\n");
printf("%d\n%d\n%d\n",a,b,c);
}
else if (c < b)
{
//second and third smallest number identified
printf("%d\n",c);
printf("%d\n",b);
printf("\n");
printf("%d\n%d\n%d\n",a,b,c);
}
}
else if (b < a && b < c)
{
//first smallest number identified
printf("%d\n",b);
if (a < c)
{
//second and third smallest number identified
printf("%d\n",a);
printf("%d\n",c);
printf("\n");
printf("%d\n%d\n%d\n",a,b,c);
}
else if (c < a)
{
//second and third smallest number identified
printf("%d\n",c);
printf("%d\n",a);
printf("\n");
printf("%d\n%d\n%d\n",a,b,c);
}
}
else if (c < a && c < b)
{
//first smallest number identified
printf ("%d\n",c);
if(a < b){
//second and third smallest number identified
printf("%d\n",a);
printf("%d\n",b);
printf("\n");
printf("%d\n%d\n%d\n",a,b,c);
}
else if (b < a)
{
//second and third smallest number identified
printf("%d\n",b);
printf("%d\n",a);
printf("\n");
printf("%d\n%d\n%d\n",a,b,c);
}
}
return 0;
}
What I am looking for: How can I simplify my solution? Will I face any problems with some conditions? If yes, then how can I handle this?