C++ Program to find largest of three numbers using function templates


In this program, You will learn how to find largest of three numbers using function templates in C++.


template < class T >

void find(T x, T y, T z) { }


Example: How to find largest of three numbers using function templates in C++

Run it Now
#include<iostream>
using namespace std;

template <class T>

int find(T x, T y, T z) {

   T lar;

   if (x > y && x > z) {
       lar = x;
   } else if (y > z) {
       lar = y;
   } else {
       lar = z;
   }
   return lar;
}

int main() {

   int x, y, z, lar;
   cout << "Enter Three Numbers :";
   cin >> x >> y>>z;

   lar = find(x, y, z);
   cout << "\nLargest is :" << lar;

   return 0;
}

Output:

Enter Three Numbers :10 20 30
Largest is :30