Program to find the smallest element among three elements

17 May, 2022
Given three numbers. The task is to find the smallest among given three numbers. 
Examples:  
Input: first = 15, second = 16, third = 10
Output: 10

Input: first = 5, second = 3, third = 6
Output: 3
 
Approach:  
Check if the first element is smaller than second and third. If yes then print it.Else if check if the second element is smaller than first and third. If yes then print it.Else third is the smallest element and print it.
Check if the first element is smaller than second and third. If yes then print it.
Else if check if the second element is smaller than first and third. If yes then print it.
Else third is the smallest element and print it.
Below is the implementation of the above approach:  
C++
C
Java
Python3
C#
PHP
Javascript
// 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 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 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..
# 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# implementation to find// the smallest of three elementsusing 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 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
<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>
5 is the smallest
 
Time complexity : O(1) 
Shashank12
jit_t
Akanksha_Rai
29AjayKumar
VishalBachchas
decode2207
kothavvsaakash
Technical Scripter 2018
Mathematical
School Programming
Technical Scripter
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Sieve of Eratosthenes
Program to find GCD or HCF of two numbers
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java