Open In App

Program to find the smallest element among three elements

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given three numbers. The task is to find the smallest among the given three numbers.
 

Examples: 

Input: first = 15, second = 16, third = 10
Output: 10

Input: first = 5, second = 3, third = 6
Output: 3

Approach:  

  1. Check if the first element is smaller than or equal to second and third. If yes then print it.
  2. Else if check if the second element is smaller than or equal to first and third. If yes then print it.
  3. Else third is the smallest element and print it.

Below is the implementation of the above approach: 
 

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>


Output

5 is the smallest

Time complexity : O(1)
Auxiliary Space: O(1)

Approach:

1. We define three variables a, b, and c with values 5, 10, and 3 respectively.
2. We use the min() function to find the smallest value among a, b, and c.
3. We assign the smallest value to a new variable min_value.
4. We use f-string to print the value of min_value.

C++




#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    int a=5, b=10, c=3;
     
    int min_value = min({a,b,c});
     
    cout<<"The smallest value is "<<min_value;
}


Java




import java.util.Arrays;
 
public class Main {
    public static void main(String[] args) {
        int a = 5, b = 10, c = 3;
         
        int min_value = Arrays.stream(new int[] {a, b, c}).min().getAsInt();
         
        System.out.println("The smallest value is " + min_value);
    }
}


Python3




a = 5
b = 10
c = 3
 
min_value = min(a, b, c)
 
print(f"The smallest value is {min_value}")


C#




using System;
 
// Declaring the class
class Program {
 
    static void Main(string[] args)
    {
 
        int a = 5, b = 10, c = 3;
 
        // Finding the minimum value
        int min_value = Math.Min(Math.Min(a, b), c);
 
        // Printing the result
        Console.WriteLine("The smallest value is "
                          + min_value);
    }
}


Javascript




const a = 5;
const b = 10;
const c = 3;
 
const min_value = Math.min(a, b, c);
 
console.log(`The smallest value is ${min_value}`);


Output

The smallest value is 3

Time complexity:
The time complexity of this program is O(1) since we are only comparing three elements, and the min() function has a constant time complexity.

Space complexity:
The space complexity of this program is O(1) since we are only using a constant amount of memory to store three variables and one additional variable to store the smallest value.

 



Last Updated : 23 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads