# Find Maximum and Minimum of two numbers using Absolute function

Last Updated : 25 Sep, 2022

Given two numbers, the task is to print the maximum and minimum of the given numbers using Absolute function.
Examples:

Input: 99, 18
Output: Maximum = 99
Minimum = 18

Input: -10, 20
Output: Maximum = 20
Minimum = -10

Input: -1, -5
Output: Maximum = -1
Minimum = -5

Approach:
This problem can be solved by applying the concept of Absolute Function and BODMAS rule

• For Maximum:

[(x + y + abs(x - y)) / 2]
• For Minimum:

[(x + y - abs(x - y)) / 2]

Let us consider two number x and y where x = 20, y = 70 respectively.
For Maximum:

[(x + y + abs(x - y)) / 2]
=> [(20 + 70)+ abs(20-70)) / 2]
=> 140 / 2 = 70 [MAXIMUM]

For Minimum:

[(x + y - abs(x - y)) / 2]
=> [(20 + 70) - abs(20-70)) / 2]
=> 40 / 2 = 20 [MINIMUM]

## C++

 // C++ program to find maximum and // minimum using Absolute function #include using namespace std;   // Function to return maximum // among the two numbers int maximum(int x, int y) {         return ((x + y + abs(x - y)) / 2); }   // Function to return minimum // among the two numbers int minimum(int x, int y) {         return ((x + y - abs(x - y)) / 2); }   // Driver code int main() {     int x = 99, y = 18;       // Displaying the maximum value     cout <<"Maximum: " << maximum(x, y) << endl;       // Displaying the minimum value     cout << "Minimum: " << minimum(x, y) << endl;     return 0; }   // This code is contributed by SHUBHAMSINGH10

## C

 // C program to find maximum and // minimum using Absolute function   #include #include   // Function to return maximum // among the two numbers int maximum(int x, int y) {         return ((x + y + abs(x - y)) / 2); }   // Function to return minimum // among the two numbers int minimum(int x, int y) {         return ((x + y - abs(x - y)) / 2); }   // Driver code void main() {     int x = 99, y = 18;       // Displaying the maximum value     printf("Maximum: %d\n", maximum(x, y));       // Displaying the minimum value     printf("Minimum: %d\n", minimum(x, y)); }

## Java

 // Java program to find maximum and // minimum using Absolute function class GFG {           // Function to return maximum     // among the two numbers     static int maximum(int x, int y)     {             return ((x + y + Math.abs(x - y)) / 2);     }           // Function to return minimum     // among the two numbers     static int minimum(int x, int y)     {             return ((x + y - Math.abs(x - y)) / 2);     }           // Driver code     public static void main (String[] args)     {         int x = 99, y = 18;               // Displaying the maximum value         System.out.println("Maximum: " + maximum(x, y));               // Displaying the minimum value         System.out.println("Minimum: " + minimum(x, y));     } }   // This code is contributed by AnkitRai01

## C#

 // C# program to find maximum and // minimum using Absolute function using System;   class GFG {           // Function to return maximum     // among the two numbers     static int maximum(int x, int y)     {             return ((x + y + Math.Abs(x - y)) / 2);     }           // Function to return minimum     // among the two numbers     static int minimum(int x, int y)     {             return ((x + y - Math.Abs(x - y)) / 2);     }           // Driver code     public static void Main()     {         int x = 99, y = 18;               // Displaying the maximum value         Console.WriteLine("Maximum: " + maximum(x, y));               // Displaying the minimum value         Console.WriteLine("Minimum: " + minimum(x, y));     } }   // This code is contributed by AnkitRai01

## Python3

 # Python3 program to find maximum and # minimum using Absolute function   # Function to return maximum # among the two numbers def maximum(x, y):         return ((x + y + abs(x - y)) // 2)   # Function to return minimum # among the two numbers def minimum(x, y):         return ((x + y - abs(x - y)) // 2)   # Driver code x = 99 y = 18   # Displaying the maximum value print("Maximum:", maximum(x, y))   # Displaying the minimum value print("Minimum:", minimum(x, y))   # This code is contributed by mohit kumar 29

## Javascript



Output:

Maximum: 99
Minimum: 18

Time complexity: O(1)
Auxiliary space: O(1)

Previous
Next