Find Maximum and Minimum of two numbers using Absolute function

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find maximum and
// minimum using Absolute function
#include <bits/stdc++.h>
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

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to find maximum and
// minimum using Absolute function
  
#include <stdio.h>
#include <stdlib.h>
  
// 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));
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

Maximum: 99
Minimum: 18



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.