Pages

Friday, May 30, 2025

C. Create a C++ program that uses a function template to find the maximum value between two values of any data type.

#include <iostream>

using namespace std;


template<typename T>

T maxVal(T a, T b) {

    return (a>b) ? a : b;

    /*

    if (a > b) {

        return a;

    } else {

        return b;

    }

    */

    /*

    OR 

    return (a > b) ? a : b is same as if 

    (a > b)

    return a;

    else

    return b;

    */

}


int main() {

    int val1, val2;

    cout << "enter first value : ";

    cin >> val1;

    cout << "\nenter second value : ";

    cin >> val2;

    int findMax = maxVal(val1, val2);

    cout << findMax << " is the max value " << endl;

    return 0;

}

No comments:

Post a Comment

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream> using namespace std; struct Node {     int data;     Node* next; }; class Stack {     Node* top; public:     Stac...