lab- 1

PHOTO EMBED

Tue Oct 24 2023 16:26:10 GMT+0000 (Coordinated Universal Time)

Saved by @Mohamedshariif #java

Write a c program to find the second smallest element in an integer array.

#include <stdio.h>
 #include <limits.h>
 
int main() {
    
    int arr[] = {1,1,1,1,1,1};
  //int arr[] = {9,5,2,8,1};
    int size = sizeof(arr) / sizeof(arr[0]);
    int smallest = INT_MAX;
    int secondSmallest = INT_MAX;

    for (int i = 0; i < size; i++) {
        if (arr[i] < smallest) {
            secondSmallest = smallest;
            smallest = arr[i];
        } 
        else if (arr[i] < secondSmallest && arr[i] != smallest) {
            secondSmallest = arr[i];
        }
    }

    if (secondSmallest == INT_MAX) {
        printf("No second smallest element found.\n");
    } 
    else {
        printf("Second smallest element: %d\n", secondSmallest);
    }

    return 0;
}
output: No second smallest element found.
//OutPUT : Second smallest element: 2
content_copyCOPY