Программа для поиска наименьшего элемента среди трех элементов

Опубликовано: 3 Декабря, 2021

Дано три числа. Задача состоит в том, чтобы среди заданных трех чисел найти наименьшее.

Примеры:

 Ввод: первый = 15, второй = 16, третий = 10
Выход: 10

Ввод: первый = 5, второй = 3, третий = 6
Выход: 3

Рекомендуется: сначала попробуйте свой подход в {IDE}, прежде чем переходить к решению.

Подход:

  1. Убедитесь, что первый элемент меньше второго и третьего. Если да, то распечатайте.
  2. В противном случае проверьте, меньше ли второй элемент, чем первый и третий. Если да, то распечатайте.
  3. Остальная третья - самый маленький элемент и распечатайте его.

Ниже представлена реализация описанного выше подхода:

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;
}

Джава

// 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>
Выход:
 5 самый маленький

Временная сложность: O (1)

Вниманию читателя! Не прекращайте учиться сейчас. Получите все важные математические концепции для соревновательного программирования с курсом Essential Maths for CP по доступной для студентов цене. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .