C++ Program to Find Smallest of Three Numbers

C++ Program to Find Smallest of Three Numbers

Finding the smallest of three numbers is a fundamental programming exercise in C++. It teaches beginners how to use conditional statements and logical thinking to compare multiple values. This concept is widely used in real-world applications, such as determining minimum scores, lowest prices, or selecting the smallest measurement in datasets. In this article, we will explore several simple and beginner-friendly ways to find the smallest number using C++.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Using Simple if-else Statements

This program uses basic if-else statements to find the smallest of three numbers. It is straightforward and perfect for beginners who want to learn basic decision-making in C++.

#include <iostream>

using namespace std;

int main() {

    int num1, num2, num3;

    cout << "Enter three numbers: " << endl;
    cin >> num1 >> num2 >> num3;

    int smallest;

    if (num1 < num2 && num1 < num3)
        smallest = num1;

    else if (num2 < num1 && num2 < num3)
        smallest = num2;

    else
        smallest = num3;

    cout << "The smallest number is: " << smallest << endl;

    return 0;

}

In this program, the if-else ladder checks each number against the others. Beginners can clearly see how conditions are evaluated step by step, making it easier to understand logical comparisons.

Program 2: Using Nested if Statements

Nested if statements offer a structured way to determine the smallest number. This method emphasizes hierarchical decision-making.

#include <iostream>

using namespace std;

int main() {

    int num1, num2, num3;

    cout << "Enter three numbers: " << endl;
    cin >> num1 >> num2 >> num3;

    int smallest;

    if (num1 < num2) {

        if (num1 < num3)
            smallest = num1;
        else
            smallest = num3;

    } else {

        if (num2 < num3)
            smallest = num2;
        else
            smallest = num3;

    }

    cout << "The smallest number is: " << smallest << endl;

    return 0;

}

Nested if statements allow you to check multiple conditions in sequence, which helps beginners understand how decisions can depend on previous comparisons.

Program 3: Using the Ternary Operator

The ternary operator provides a concise way to compare values. This program demonstrates how to find the smallest number using just one line of code.

#include <iostream>

using namespace std;

int main() {

    int num1, num2, num3;

    cout << "Enter three numbers: " << endl;
    cin >> num1 >> num2 >> num3;

    int smallest = (num1 < num2) ? (num1 < num3 ? num1 : num3) : (num2 < num3 ? num2 : num3);

    cout << "The smallest number is: " << smallest << endl;

    return 0;

}

The ternary operator makes code concise and readable for simple comparisons. Beginners can use it to write compact programs without multiple if-else statements.

Program 4: Using min Function from <algorithm>

C++ provides the min function in the <algorithm> library, making it easy to find the smallest value among multiple numbers.

#include <iostream>
#include <algorithm>

using namespace std;

int main() {

    int num1, num2, num3;

    cout << "Enter three numbers: " << endl;
    cin >> num1 >> num2 >> num3;

    int smallest = min(num1, min(num2, num3));

    /** 

        using initializer list (C++11 and above)

        int smallest = min({num1, num2, num3});

    */

    cout << "The smallest number is: " << smallest << endl;

    return 0;

}

This approach is practical for beginners who want to use standard library functions. It avoids manual comparisons and produces clean, efficient code.

Frequently Asked Questions (FAQ)

Here are some common questions beginners often ask about finding the smallest number in C++:

Q1: What if two numbers are equal and the smallest?
The program will return any of the numbers that are equal and smallest. The logic still works correctly.

Q2: What if all three numbers are the same?
If all numbers are equal, the program will correctly identify that number as the smallest.

Q3: Can we find the smallest number in a list of numbers?
Yes, using arrays or vectors, we can iterate through all elements and use loops or the min_element function from <algorithm>.

Q4: Is using the ternary operator faster than if-else?
There’s no significant difference in speed for small programs, but the ternary operator can make your code more concise and easier to read.

Conclusion

Finding the smallest of three numbers is a key programming skill that helps beginners learn logical decision-making. We explored four methods: using simple if-else statements, nested if, the ternary operator, and the min function. Each method provides a clear and practical way to solve the problem. Practicing these techniques will improve your understanding of conditional logic and prepare you for more advanced programming tasks.

Additional & References

To continue learning, beginners should practice using arrays, vectors, and standard library functions for finding minimum values in larger datasets. Experimenting with these approaches will strengthen your problem-solving skills in C++.

Scroll to Top