C program to find first & second largest number in array


 /*

C program to find first & second largest number in array

        Array: 3, 10, 80, 20, 5

        Max1: 80

        Max2: 20


Logic:

        1. Input size and elements in array, store it in some variable say size and arr.

        2. Declare two variables max1 and max2 to store first and second largest elements.

        Store minimum integer value in both i.e. max1 = max2 = INT_MIN.

        3. Iterate though all array elements, run a loop from 0 to size - 1.

        4. Inside loop, check if arr[i] > max1

                max2 = max1

                max1 = arr[i]

        5. Else if arr[i] > max2 && arr[i] < max1

                max2 = arr[i]

*/

#include<stdio.h>

#include <limits.h>

int main()

{

        int size, arr[100], i, max1, max2;

        scanf("%d", &size);

        for(i = 0; i < size; i++){

                scanf("%d", &arr[i]);

        }

        max1 = max2 = INT_MIN;

        for(i = 0; i < size; i++){

                if(arr[i] > max1){

                        max2 = max1;

                        max1 = arr[i];

                }

                else if(arr[i] > max2 && arr[i] < max1){

                        max2 = arr[i];

                }

        }

        printf("%d %d\n", max1, max2);

        return 0;

}


Video Description: https://www.youtube.com/watch?v=6276kGaKEI4




1 comment:

  1. I really thank you for the valuable info on this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! All the best!
    Php projects with source code
    Online examination system in php
    Student management system in php
    Php projects for students
    Free source code for academic
    Academic projects provider in nashik
    Academic project free download

    ReplyDelete