codescracker


c

C Program to Find Smallest of Three Numbers



« Previous Program Next Program »

Here we will learn about how to find and print smallest among any given three numbers (by user at run-time) with and without using any user-defined function. At last of the tutorial, we will also see how to find out and print the smallest of three numbers using Ternary operator.

Find Smallest of Three Numbers in C

Here is the program that will find out the smallest of three numbers provided by user at run-time.

// Write a program in C to find and print the
// smallest number among given three numbers
// --------codescracker.com----------

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

The program was written under Code::Blocks IDE, here is the first snapshot of the sample run after successful build and run:

c find smallest among three numbers

Supply any three numbers and press ENTER key to see the smallest number among the given three numbers as shown here in the snapshot given below:

find smallest of three numbers c

Here are some of the main steps used in above program:

Here is another program to find out the smallest of any three numbers in too simple way. This program is simple and easy to understand:

// Write a program in C to find smallest
// of three numbers in simple way
// --------codescracker.com----------

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

Here is the final snapshot of the above program's sample run:

c print smallest of three numbers

In above program, we have check that whether first number say a is less than both the other number say b and c or not. If it is, then we have initialized the value of a to small. In this way, we have used the logic to find out the smallest of three numbers in simple way.

C Find Smallest of Three Numbers using Function

Let's create another program that will do the same task as the above two program has done but using user-defined function. Here is the program that uses a user-defined function to return the smallest among any three numbers:

// Write a program in C to print smallest
// of three numbers using user-defined
// function
// --------codescracker.com----------

#include<stdio.h>
#include<conio.h>
int getSmall(int, int, int);
int main()
{
    int a, b, c, small;
    printf("Enter three numbers : ");
    scanf("%d%d%d", &a, &b, &c);
    small = getSmall(a, b, c);
    printf("\nSmallest = %d", small);
    getch();
    return 0;
}
int getSmall(int num1, int num2, int num3)
{
    if(num1<num2)
    {
        if(num2<num3)
            return num1;
        else
        {
            if(num1<num3)
                return num1;
            else
                 return num3;
        }
    }
    else
    {
        if(num2<num3)
            return num2;
        else
            return num3;
    }
}

Here is the final snapshot of the sample run:

c find smallest of three using function

C Find Smallest of Three Numbers using Ternary Operator

Let's create another program that will also find and print the smallest of three numbers but this time using Ternary operator as shown in the program given below:

// Write a program in C calculate smallest 
// of three numbers using Ternary Operator
// --------codescracker.com----------

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

Here is the final snapshot of the sample run:

print smallest of three using ternary c

The main logical code is:

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

The description of above one line logical code is given below:


« Previous Program Next Program »






Tools
Calculator

Quick Links
Signup - Login - Give Online Test