xxxxxxxxxx
const findSmallestInt = (args) => {
return Math.min(args)
}
xxxxxxxxxx
static int SmallestNumberin(int[] numbers){
int smallestNumber = numbers[0];
for (int i = 1; i < numbers.Length +1; i++)
{
if(numbers[i] < smallestNumber){
smallestNumber = numbers[i];
}
}
return smallestNumber;
}
xxxxxxxxxx
// smallest number from array
function solution(num) {
let small = num[0];
for (let i = 1; i <= num.length; i++) {
if (small > num[i]) {
small = num[i]
}
}
console.log("smallest nunber:", small)
}
solution([3, 1, 4, 6, 5, 7, 3, 2,0,-1])
xxxxxxxxxx
A: class SmallestNumberInAnArray {
public static void main(String[] args) {
int [] num = {9,3,4,2,7};
int tem = num[0];
for(int i=0;i<num.length;i++){
if(num[i]<tem){
tem=num[i];
}
}
System.out.println("Smallest number in an array - "+tem);
}
}
xxxxxxxxxx
# Define a function to find the smallest number in an array
def find_smallest_number(arr):
if len(arr) == 0:
return None # Return None if the array is empty
smallest = arr[0] # Assume the first element is the smallest
for num in arr:
if num < smallest:
smallest = num # Update smallest if a smaller number is found
return smallest
# Example usage
numbers = [5, 2, 9, 1, 7]
smallest_number = find_smallest_number(numbers)
print("The smallest number in the array is:", smallest_number)
xxxxxxxxxx
#find the smallest element in an array
#Approach:
#Sort the array in ascending order.
arr = [2,5,1,3,0]
# sorted method
my_arr = sorted(arr)
print(f"The smallest number is {my_arr[0]}")
#* USING FOR LOOP
smallest = arr[0]
for i in range(0,len(arr)):
if arr[i] < smallest :
smallest = arr[i]
print(f"The smallest number is {smallest}")