TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to print the smallest element present in an array

Program to print the smallest element present in an array on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Q. Program to print the smallest element present in an array.

Explanation

In this program, we need to find out the smallest element present in the array. This can be achieved by maintaining a variable min which initially will hold the value of the first element. Loop through the array by comparing the value of min with elements of the array. If any of the element's value is less than min, store the value of the element in min.

Program to print the smallest element present in an array.

Consider above array. Initially, min will hold the value 25. In the 1st iteration, min will be compared with 11. Since 11 is less than 25. Min will hold the value 11. In a 2nd iteration, 11 will be compared with 7. Now, 7 is less than 11. So, min will take the value 7. Continue this process until the end of the array is reached. At last, min will hold the smallest value element in the array.

Algorithm

  1. Declare and initialize an array.
  2. Store first element in the variable min.
  3. Loop through the array from 0 to length of the array and compare the value of min with elements of the array.
  4. If any element is less than min, min will hold the value of that element.
  5. At last, min will represent the smallest element in the array.

Solution

Python

#Initialize array 
arr = [25, 11, 7, 75, 56]; 
 
#Initialize min with the first element of the array.

min = arr[0];
 
#Loop through the array
for i in range(0, len(arr)):
    #Compare elements of array with min
   if(arr[i] < min):
       min = arr[i];
 
print("Smallest element present in given array: " + str(min));

Output:

Smallest element present in given array: 7

C

#include <stdio.h>
 
int main()
{
    //Initialize array 
    int arr[] = {25, 11, 7, 75, 56};  
    
    //Calculate length of array arr
    int length = sizeof(arr)/sizeof(arr[0]);
    
    //Initialize min with first element of array.
    int min = arr[0];
    
    //Loop through the array
    for (int i = 0; i < length; i++) { 
        //Compare elements of array with min
       if(arr[i] < min)
           min = arr[i];
    }  
    printf("Smallest element present in given array: %d\n", min);
    return 0;
}

Output:

Smallest element present in given array: 7

JAVA

public class SmallestElement {
    public static void main(String[] args) {    
        
        //Initialize array 
        int [] arr = new int [] {25, 11, 7, 75, 56}; 
        
        //Initialize min with first element of array.
        int min = arr[0];
        
        //Loop through the array
        for (int i = 0; i < arr.length; i++) { 
            //Compare elements of array with min
           if(arr[i] < min)
               min = arr[i];
        }  
        System.out.println("Smallest element present in given array: " + min);
    }
}

Output:

Smallest element present in given array: 7

C#

using System;
                    
public class SmallestElement
{
    public static void Main()
    {
        //Initialize array 
        int [] arr = new int [] {25, 11, 7, 75, 56}; 
        
        //Initialize min with first element of array.
        int min = arr[0];
        
        //Loop through the array
        for (int i = 0; i < arr.Length; i++) { 
            //Compare elements of array with min
           if(arr[i] < min)
               min = arr[i];
        }  
        Console.WriteLine("Smallest element present in given array: " + min);
    }
}

Output:

Smallest element present in given array: 7

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//Initialize array 
$arr = array(25, 11, 7, 75, 56); 
 
//Initialize min with first element of array.
$min = $arr[0];
 
//Loop through the array
for ($i = 0; $i < count($arr); $i++) { 
    //Compare elements of array with min
   if($arr[$i] < $min)
       $min = $arr[$i];
}  
print("Smallest element present in given array: " . $min);
?>
</body>
</html> 

Output:

Smallest element present in given array: 7

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf