write a complete C program that will ask a user for 3 integer numbers and will print out on the screen the two smallest values of the 3.

1 Answer | Add Yours

tiburtius's profile pic

tiburtius | High School Teacher | (Level 2) Educator

Posted on

With arrays and function sort:


#include<stdio.h>
#include<algorithm> //for sort()

int main()
{
int a[3],i;
printf("Input 3 integers: ");
for(i=0;i<3;i++)
{
scanf("%d",&a[i]);
}
std::sort(&a[0],&a[3]);//sorts elements between a[0] and a[3] not including a[3]
 printf("Two smallest integers are %d and %d.",a[0],a[1]);

return 0;

}

Without arrays and sort:

#include<stdio.h>

int main()
{

int a,b,c;
printf("Input 3 integers: ");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
if(a>c)
printf("Two smallest integers are %d and %d.",b,c);
else
printf("Two smallest integers are %d and %d.",b,a);
else if(b>c)
printf("Two smallest integers are %d and %d.",a,c);
else
printf("Two smallest integers are %d and %d.",a,b);
return 0;

}

We’ve answered 318,911 questions. We can answer yours, too.

Ask a question