Find elements

Cpp program:find smallest of three numbers using function

Cpp program:find smallest of three numbers using function

In this tutorial, we will discuss the concept of Cpp program:find smallest of three numbers using the function.

In this post, we will learn how to find the smallest number among three numbers using a user-defined function in the C++ programming language

Cpp program: find the smallest

 

In my previous post, I have explained the various approaches to find the smallest number of among  three numbers in C++ language

However, in this program, we embed the logic of the “find the smallest among three number” program in the function.

Here, I have explained how to find the smallest number from the given three number and how to embed this logic in the function.

Program 1

Find the smallest of three numbers using Nested if statements

This program allows the user to enter three numbers and compare to select the smallest number using nested if  statements

 

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int biggestNum(int, int,int);
int main()
{
    int num1,num2,num3; //declare the variables
    cout<<"Enter the first number:  ";
    cin>>num1;//get input from user for num1
    cout<<"Enter the second number: ";
    cin>>num2;//get input from user for num2
    cout<<"Enter the third number: ";
    cin>>num3;//get input from user for num3
    biggestNum(num1,num2,num3);
    getch();
    return 0;
}

    int biggestNum(int num1,int num2,int num3){
    if(num1<num2){//compare num1 and num2
            if(num1<num3){//compare num1 and num3
         cout<<"\nSmallest number is:" <<num1;
            }
            else{
                cout<<"\nSmallest number is:"<<num3;
            }
    }
    else{
        if(num2<num3){//compare num2 and num1
            cout<<"\nSmallest number is:"<<num2;
        }
        else{
            cout<<"\nSmallest number is:"<<num3;
        }

    }
    }

When the above code is executed, it produces the following results

Enter the first number: 56
Enter the second number: 23
Enter the third number: 69

The smallest number is: 23

Program 2

Find the smallest of three numbers using if else if statements

This program allows the user to enter three numbers and compare to select the smallest number using if else if statements

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int biggestNum(int, int,int);
int main()
{
    int num1,num2,num3; //declare the variables
    cout<<"Enter the first number:  ";
    cin>>num1;//get input from user for num1
    cout<<"Enter the second number: ";
    cin>>num2;//get input from user for num2
    cout<<"Enter the third number: ";
    cin>>num3;//get input from user for num3
    biggestNum(num1,num2,num3);
    getch();
    return 0;
}

    int biggestNum(int num1,int num2,int num3){
    if(num1<num2&&num1<num3){
         cout<<"\nSmallest number is:" <<num1;
            }
    else if(num2<num3){//compare num2 and num1
            cout<<"\nSmallest number is:"<<num2;
        }
        else{
            cout<<"\nSmallest number is:"<<num3;
        }

    }


When the above code is executed, it produces the following results

Enter the first number: 23
Enter the second number: 78
Enter the third number: 54

Smallest number is:23

 

Smiler post

C program to find the smallest among three numbers using the function

Python code to find the smallest of three numbers using the method

Java code to find the smallest of three numbers using the function

 

Suggested for you

Operator in C++ language

If statements in C++ language

Function in C++ language

Data types in C++

variables in C++

 

Python program:find greatest of three numbers using function
Java program:find smallest of three numbers using method
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago