C Program to Find the Smallest of Three Numbers

In this article, we will write a C program to find the smallest of three numbers. The program takes three numbers from the user as input, finds the smallest among them and prints the result on the output screen.

Sample Example:

Enter three numbers: 20 15 30
The smallest among 20, 15 & 30 is: 15

Enter three numbers: 10 20 -15
The smallest among 10, 20 & -15 is: -15

To find the smallest among three numbers, we can use the following algorithm:

  1. Check if the first number is less than the second.
  2. If yes, it means either the first number is the smallest or the third number is the smallest.
    • Check if the first number is also less than the third number. If yes, it means the first number is the smallest. Otherwise, the third number is the smallest.
  3. If no, it means either the second number is the smallest or the third number is the smallest.
    • Check if the second number is also less than the third number. If yes, it means it is the smallest. Otherwise, the third number is the smallest.

To implement the above algorithm in C, we can use simple if...else statements. However, these if...else blocks will be nested.

See this example:

// C program to find the smallest among three numbers

#include <stdio.h>

int main() {
    
    int a, b, c, smallest;
    
    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);
    
    if(a < b){
        if(a < c){
            smallest = a;
        }
        else{
            smallest = c;
        }
    }
    else{
        if(b < c){
            smallest = b;
        }
        else{
            smallest = c;
        }
    }
    
    printf("The smallest among %d, %d & %d is: %d", a, b, c, smallest);
    
    return 0;

}

Output:

Enter three numbers: 20 10 30
The smallest among 20, 10 & 30 is: 10

This approach works for positive and negative both types of numbers.


2. Find Smallest Among Three Numbers Without Using Nested If Else Blocks

We can also find the smallest of three numbers without using the nested If Else blocks.

Here is the algorithm that we can use:

  1. Check if the first number is less than the second and third numbers. If yes, it is the smallest.
  2. Otherwise, check if the second number is less than the first and third numbers. If yes, it is the smallest.
  3. If not, then the third number is the smallest among all.

See implementation in the following program:

// C program to find the smallest among three numbers

#include <stdio.h>

int main() {
    
    int a, b, c, smallest;
    
    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);
    
    if(a <= b && a <= c){
        smallest = a;
    }
    else if(b <= a && b <= c){
        smallest = b;
    }
    else{
        smallest = c;
    }
    
    printf("The smallest among %d, %d & %d is: %d", a, b, c, smallest);
    
    return 0;

}

Output:

Enter three numbers: 100 50 150
The smallest among 100, 50 & 150 is: 50

You can use either of the two approaches whichever you like the most. The end result will be the same in both cases.

Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts

Leave a Comment