Dados dos números, la tarea es imprimir el máximo y el mínimo de los números dados usando la función Absoluta.
Ejemplos:
Input: 99, 18 Output: Maximum = 99 Minimum = 18 Input: -10, 20 Output: Maximum = 20 Minimum = -10 Input: -1, -5 Output: Maximum = -1 Minimum = -5
Planteamiento:
Este problema se puede resolver aplicando el concepto de Función Absoluta y la regla BODMAS .
- Para Máximo:
[(x + y + abs(x - y)) / 2]
- Para mínimo:
[(x + y - abs(x - y)) / 2]
Consideremos dos números x e y donde x = 20, y = 70 respectivamente.
Para Máximo:
[(x + y + abs(x - y)) / 2] => [(20 + 70)+ abs(20-70)) / 2] => 140 / 2 = 70 [MAXIMUM]
Para mínimo:
[(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 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 <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)); }
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
<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>
Producción:
Maximum: 99 Minimum: 18
Publicación traducida automáticamente
Artículo escrito por Vishesh__Jha y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA