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++
// C++ program to find maximum and// minimum using Absolute function#include <bits/stdc++.h>using namespace std;// Function to return maximum// among the two numbersint maximum(int x, int y){ return ((x + y + abs(x - y)) / 2);}// Function to return minimum// among the two numbersint minimum(int x, int y){ return ((x + y - abs(x - y)) / 2);}// Driver codeint 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 <stdio.h>#include <stdlib.h>// Function to return maximum// among the two numbersint maximum(int x, int y){ return ((x + y + abs(x - y)) / 2);}// Function to return minimum// among the two numbersint minimum(int x, int y){ return ((x + y - abs(x - y)) / 2);}// Driver codevoid 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 functionclass 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 functionusing 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 numbersdef maximum(x, y): return ((x + y + abs(x - y)) // 2)# Function to return minimum# among the two numbersdef minimum(x, y): return ((x + y - abs(x - y)) // 2)# Driver codex = 99y = 18# Displaying the maximum valueprint("Maximum:", maximum(x, y))# Displaying the minimum valueprint("Minimum:", minimum(x, y))# This code is contributed by mohit kumar 29 |
Javascript
<script>// JavaScript program to find maximum and// minimum using Absolute function // Function to return maximum // among the two numbers function maximum(x,y) { return ((x + y + Math.abs(x - y)) / 2); } // Function to return minimum // among the two numbers function minimum(x,y) { return ((x + y - Math.abs(x - y)) / 2); } // Driver code let x = 99, y = 18; // Displaying the maximum value document.write("Maximum: " + maximum(x, y)+"<br>"); // Displaying the minimum value document.write("Minimum: " + minimum(x, y)); // This code is contributed by sravan kumar</script> |
Output:
Maximum: 99 Minimum: 18
Time complexity: O(1)
Auxiliary space: O(1)


Please Login to comment...