C++ Program to Find Maximum of Two Numbers and of Array

Problem:

Write a C++ program to find the maximum of two numbers. Write a C program to find the maximum number in an array.

C++ Program / Source Code:

Here is the source code of C++ program to find maximum of two numbers and of array
/* Aim: Write a C++ program to find maximum of two numbers and of array */

#include<iostream>
using namespace std;

class maxdata
{
 public:

// C++ code to find maximum of two numbers 
 int max(int x,int y)
 {
  return ((x>y)?x:y);
 }

// C++ code to find maximum number in an array 
 int max(int a[5])
 {
  int mx=a[0];

  for(int i=1;i<5;i++)
  {
   if(mx<a[i])
    mx=a[i];
  }
  return mx;
 }
};

int main()
{
 maxdata M1;
 int a[5]={4,45,120,2,16};

 cout<<"Max element out of (10,20) is "<<M1.max(10,20)<<endl;
 cout<<"Max element out of array (4,45,120,16) is "<<M1.max(a)<<endl;

 return 0;
}

/* Output of above code:-

Max element out of (10,20) is 20
Max element out of array (4,45,120,16) is 120

*/