F Smallest of two numbers in CPP using Function | CodeTheta

Smallest of two numbers in CPP using Function

July 25, 2019

1. Finding smallest number using if else condition.
Code :
#include<iostream>
#include<conio.h>
using namespace std;

int SmallestFunction(int, int);

int main() {
    int n1, n2, SmallestNumber;
    cout<<"Enter First Number: ";
    cin>>n1;
    cout<<"Enter Second Number: ";
    cin>>n2;   
    SmallestNumber = SmallestFunction(n1, n2);
    cout<<"Smallest Number is: "<<SmallestNumber;
}

int SmallestFunction(int x, int y) {
    if(x<y) {
        return x;
    }
    else {
        return y;
    }
}


Output :
Enter First Number:  7
Enter Second Number: 3
Smallest Number is: 3

2. Finding smallest number using Ternary Operator.
Code :
#include<iostream>
#include<conio.h>
using namespace std;

int SmallestFunction(int, int);

int main() {
    int n1, n2, SmallestNumber;
    cout<<"Enter First Number: ";
    cin>>n1;
    cout<<"Enter Second Number: ";
    cin>>n2;  
    SmallestNumber = SmallestFunction(n1, n2);
    cout<<"Smallest Number is: "<<SmallestNumber;
}

int SmallestFunction(int x, int y) {
    return ( x < y ) ? x : y;
}


Output :
Enter First Number:  4
Enter Second Number: 3
Smallest Number is: 3

IDE Used To Test This Code : DEV C++.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

Post a Comment