Press "Enter" to skip to content

C++ Program to Find Smallest Among Given Three Numbers

YOC already wrote an article about a program to find biggest among given three numbers. Here we find smallest among given three numbers. The logic is just opposite, that is every greater than symbol (>) is replaced with less than (<) symbol and all remaining codes are same.

As we read three solutions on finding largest among three numbers, this program has three or more solutions for getting smallest number. Code explanation is similar to the code explanation of finding biggest number.

Solution 1:

#include <iostream>
using namespace std;
int main()
{
    int a,b,c,smallest;
    cout << "Enter three numbers :";
    cin>>a>>b>>c;
    if(a<b &amp;&amp; a<c)
        cout<<"smallest is "<<a;
    else if(b<a &amp;&amp; b<c)
        cout<<"smallest is "<<b;
    else
    cout<<"Smallest number is "<< c;
    return 0;
}

Solution 2:

#include <iostream>
using namespace std;
int main()
{
    int a,b,c,smallest;
    cout << "Type any three numbers :";
    cin>>a>>b>>c;
    if(a<b)
    {
      if(a<c)
        smallest = a;
      else
        smallest = c;
    }
    else if(b<c)
        smallest = b;
    else
        smallest = c;
    cout<<"Smallest number is "<< smallest;
    return 0;
}

Solution 3:

#include <iostream>
using namespace std;
int main()
{
    int a,b,c,smallest;
    cout << "Enter any three numbers :";
    cin>>a>>b>>c;
    smallest = a;
    if(b<smallest)
        smallest = b;
    if(c<smallest)
        smallest = c;
        cout<<"Smallest number is "<< smallest;
    return 0;
}

Sample Output:

C++ Program to find Smallest among given three numbers
18

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *