# C Program to Find Smallest between Two Numbers

Here we will learn about how to find and print smallest between any two given number (by user at run-time) with and without using user-defined function. At last, we will also use Ternary operator to find and print smallest between the two numbers.

## Find Smallest of Two Numbers in C

Here is the program that will ask from the user to enter any two number to find out and print the smallest one as shown here:

```// Write a program in C to find smallest
// between any two given number by user
// ------------codescracker.com------------

#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, small;
printf("Enter any two number: ");
scanf("%d%d", &a, &b);
if(a<b)
small=a;
else
small=b;
printf("\nSmallest of the two number is: %d", small);
getch();
return 0;
}```

The program was written under Code::Blocks IDE, therefore after successful build and run, here is the first snapshot of the sample run of above program: Supply any two number and press ENTER key to see the smallest number between the given two numbers as shown here in the second snapshot: Here are some of the main steps used in the above program:

• Receive any two number as input
• Use if statement to check whether the first number is less than the second number or not
• If it is, then initialize the first number to any variable say small
• If it does not, then go to the else block and initialize the value of second number to any variable say small
• Print the value of small, that will be the smallest number between the given two

## C Function to return Smallest of two Numbers

Here is another program using function. In this program we have created a function that will takes the two given number as arguments and will return the smallest one between both the number or argument.

```// Write a program in C using function that
// takes 2 numbers as argument and returns
// the smaller value between the two numbers
// -----------codescracker.com-----------

#include<stdio.h>
#include<conio.h>
int findSmall(int, int);
int main()
{
int num1, num2, small;
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);
small = findSmall(num1, num2);
printf("Smallest = %d", small);
getch();
return 0;
}
int findSmall(int a, int b)
{
if(a>b)
return b;
else
return a;
}```

Here is the final snapshot of the sample run: ## C Find Smallest of Two using Ternary Operator

Now let's create another program which will also do the same task as the above two program has done, that is to find out and print the smallest of any two number, but this time I'll use the Ternary operator here in this case.

This is the shortest program to find out smallest number between any two number. As we have used Ternary operator here.

```// Write a program in C to find smallest
// of any two number using Ternary Operator
// ------------codescracker.com------------

#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, small;
printf("Enter any two number: ");
scanf("%d%d", &a, &b);
small = (a<b) ? a : b;
printf("\nSmallest = %d", small);
getch();
return 0;
}```

Here is the final snapshot of the sample run of this program: The main logic of the above program is:

`small = (a<b) ? a : b;`

Here, if the expression a<b (if a is less than b) is true, then the value of a will be initialized to small. Otherwise the value of b will be initialized to small. That is, if a is less than b, then the expression will become:

`small = a;`

Otherwise, if a is not less than b or b is less than a, then the expression will become:

`small = b;`

Tools
Calculator