Dados tres números. La tarea es encontrar el más pequeño entre los tres números dados.
Ejemplos:
Input: first = 15, second = 16, third = 10 Output: 10 Input: first = 5, second = 3, third = 6 Output: 3
Acercarse:
- Compruebe si el primer elemento es más pequeño que el segundo y el tercero. Si es así, imprímelo.
- De lo contrario, compruebe si el segundo elemento es más pequeño que el primero y el tercero. Si es así, imprímelo.
- De lo contrario, el tercero es el elemento más pequeño e imprimirlo.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to find // the smallest of three elements #include <bits/stdc++.h> using namespace std; int main() { int a = 5, b = 7, c = 10; if (a <= b && a <= c) cout << a << " is the smallest"; else if (b <= a && b <= c) cout << b << " is the smallest"; else cout << c << " is the smallest"; return 0; }
C
// C implementation to find // the smallest of three elements #include <stdio.h> int main() { int a = 5, b = 7, c = 10; if (a <= b && a <= c) printf("%d is the smallest",a); else if (b <= a && b <= c) printf("%d is the smallest",b); else printf("%d is the smallest",c); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java implementation to find // the smallest of three elements import java.io.*; class GFG { public static void main (String[] args) { int a = 5, b = 7, c = 10; if (a <= b && a <= c) System.out.println( a + " is the smallest"); else if (b <= a && b <= c) System.out.println( b + " is the smallest"); else System.out.println( c + " is the smallest"); } } // This code is contributed by shs..
Python3
# Python implementation to find # the smallest of three elements a, b, c = 5, 7, 10 if(a <= b and a <= c): print(a, "is the smallest") elif(b <= a and b <= c): print(b, "is the smallest") else: print(c, "is the smallest") # This code is contributed # by 29AjayKumar
C#
// C# implementation to find // the smallest of three elements using System; class GFG { static public void Main () { int a = 5, b = 7, c = 10; if (a <= b && a <= c) Console.WriteLine( a + " is the smallest"); else if (b <= a && b <= c) Console.WriteLine( b + " is the smallest"); else Console.WriteLine( c + " is the smallest"); } } // This code is contributed by jit_t
PHP
<?php // PHP implementation to find // the smallest of three elements // Driver Code $a = 5; $b = 7; $c = 10; if ($a <= $b && $a <= $c) echo $a . " is the smallest"; else if ($b <= $a && $b <= $c) echo $b . " is the smallest"; else echo $c . " is the smallest"; // This code is contributed // by Akanksha Rai
Javascript
<script> // Javascript implementation to find // the smallest of three elements let a = 5, b = 7, c = 10; if (a <= b && a <= c) document.write( a + " is the smallest"); else if (b <= a && b <= c) document.write( b + " is the smallest"); else document.write( c + " is the smallest"); </script>
Producción:
5 is the smallest
Complejidad del tiempo : O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por VishalBachchas y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA