C Program To Find First and Second Biggest Element In An Array

Lets write a C program to find first and second biggest element/number in an array, without sorting it.

Related Read:
Find First and Second Biggest In An Array, Without Sorting It: C

Example: Expected Output

Enter 5 unique integer numbers
5
2
6
4
3
First Big: 6
Second Big: 5
array with size 5

Important Note:
This code only works for inputs which has unique integer numbers. If there are duplicate integer numbers which is biggest in the array, then it’ll show the same number for both first and second biggest element. We can fix it, but it’s out of scope of this problem statement. Just know the limitation of this code.

Ex: a[5] = {2, 4, 5, 3, 5};
In this case, both first big and second big will have value 5.

Video Tutorial: C Program To Find First and Second Biggest Element In An Array


[youtube https://www.youtube.com/watch?v=iBqz2ZmQHSE]

YouTube Link: https://www.youtube.com/watch?v=iBqz2ZmQHSE [Watch the Video In Full Screen.]

Source Code: C Program To Find First and Second Biggest Element In An Array

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, fbig, sbig;

    if(N < 3)
    {
        printf("Please have an array with at least 2 elements\n");
        return(0);
    }

    printf("Enter %d unique integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    (a[0] > a[1]) ? (fbig = a[0], sbig = a[1]) :
                    (fbig = a[1], sbig = a[0]);

    for(i = 2; i < N; i++)
    {
        if(fbig < a[i])
        {
            sbig = fbig;
            fbig = a[i];
        }
        else if(sbig < a[i])
        {
            sbig = a[i];
        }
    }

    printf("First Big: %d\nSecond Big: %d\n", fbig, sbig);

    return 0;
}

Output 1:
Enter 5 unique integer numbers
5
2
6
4
3
First Big: 6
Second Big: 5

Output 2:
Enter 5 unique integer numbers
2
4
5
7
9
First Big: 9
Second Big: 7

In above source code we’re making use of macros to assign array size.

Logic To Find First and Second Biggest In An Array

First we assign biggest of a[0] and a[1] to variable fbig and the second biggest to sbig.
1. Using if else

    if(a[0] > a[1])
    {
        fbig = a[0];
        sbig = a[1];
    }
    else
    {
        fbig = a[1];
        sbig = a[0];
    }

2. Using Ternary/Conditional Operators

    (a[0] > a[1]) ? (fbig = a[0], sbig = a[1]) :
                    (fbig = a[1], sbig = a[0]);

In both the cases the logic is same. If a[0] is greater than a[1], then value present at a[0] will be assigned to fbig, and a[1] will be assigned to sbig. If a[0] is not greater than a[1], then a[1] will be assigned to fbig and a[0] will be assigned to sbig.

Related Read:
Biggest of Two Numbers Using Ternary Operator: C

for loop
Since both a[0] and a[1] are already sorted out, the comparison must start from a[2]. So we initialize the loop counter variable i to 2 and loop through until i < N and for each iteration we increment the value of i by 1.

Inside for loop
If value of fbig is less than the fetched element of the array, then we transfer the value of fbig to sbig and assign the new array element value to fbig.

Else if, the fetched element of the array is less than fbig but greater than sbig, then we assign the value of the array element to sbig.

    for(i = 2; i < N; i++)
    {
        if(fbig < a[i])
        {
            sbig = fbig;
            fbig = a[i];
        }
        else if(sbig < a[i])
        {
            sbig = a[i];
        }
    }

Once control exits for loop, we print the values present in fbig and sbig.

Explanation With Example

If int a[6] = {2, 4, 5, 7, 9, 8};
Here a[0] = 2 and a[1] = 4;
After executing below code:

    (a[0] > a[1]) ? (fbig = a[0], sbig = a[1]) :
                    (fbig = a[1], sbig = a[0]);

fbig will have a[1], which is 4.
sbig will have a[0], which is 2.

indexa[index]sbigfbig
2a[2] = 545
3a[3] = 757
4a[4] = 979
5a[5] = 88

At the end of for loop, fbig will have 9 and sbig will have 8.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find First and Second Biggest in N Numbers, without using Arrays, using For Loop

Write a C program to find first and second biggest numbers in a list of N numbers entered by the user, without using arrays and without sorting the list and by using for loop.

Related Read:
For Loop In C Programming Language
Find First and Second Biggest in N Numbers, without using Arrays: C Program

Video Tutorial: C Program To Find First and Second Biggest in N Numbers, without using Arrays, using For Loop


[youtube https://www.youtube.com/watch?v=EtdVsh9XhS8]

YouTube Link: https://www.youtube.com/watch?v=EtdVsh9XhS8 [Watch the Video In Full Screen.]


Source Code: C Program To Find First and Second Biggest in N Numbers, without using Arrays, using For Loop

#include<stdio.h>

int main()
{
    int limit, num, count, fbig = 0, sbig = 0;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d positive numbers\n", limit);

    for(count = 1; count <= limit; count++)
    {
        scanf("%d", &num);

        if(num > fbig)
        {
            sbig = fbig;
            fbig = num;
        }

        if(num > sbig && num < fbig)
        {
            sbig = num;
        }
    }

    printf("First big = %d\nSecond Big = %d\n", fbig, sbig);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 positive numbers
1
9
5
3
8
First big = 9
Second Big = 8

Output 2:
Enter the limit
8
Enter 8 positive numbers
1
5
9
3
7
8
6
10
First big = 10
Second Big = 9

Logic To Find First and Second Biggest Number in N Numbers, without using Arrays

First we ask the user to enter length of numbers list. If user enters limit value as 5, then we ask the user to enter 5 numbers. Once the user enters limit value, we iterate the for loop until loop counter variable count is less than or equal to limit. For each iteration of for loop we ask the user to enter a positive integer number. We check if the new number entered by the user is greater than fbig. If true, we swap the value of fbig to sbig and value of num to fbig.

Next we check if the user entered number is greater than sbig and less than fbig, if true, we assign the value of num to sbig.

Once control exits for loop, inside fbig we will have biggest number from the list of numbers entered by the user and sbig will have the second biggest number from the list of numbers entered by the user.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Find First and Second Biggest in N Numbers, without using Arrays: C Program

Write a C program to find first and second biggest numbers in list of N numbers without using arrays and without sorting the list and by using while loop.

Related Read:
while loop in C programming
Find Biggest of N Numbers, without using Arrays: C Program

Source Code: Find First and Second Biggest in N Numbers, without using Arrays: C Program

 
#include<stdio.h>

int main()
{
    int limit, num, fbig = 0, sbig = 0;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d positive numbers\n", limit);
 

    while(limit > 0)
    {
        scanf("%d", &num);

        if(num > fbig)
        {
            sbig = fbig;
            fbig = num;
        }
        if(num > sbig && num < fbig)
        {
            sbig = num;
        }

        limit--;
    }

    printf("First Big is %d\n", fbig);
    printf("Second Big is %d\n", sbig);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5
First Big is 5
Second Big is 4

Output 2:
Enter the limit
5
Enter 5 numbers
5
4
3
2
1
First Big is 5
Second Big is 4

Output 3:
Enter the limit
5
Enter 5 numbers
1
4
3
5
2
First Big is 5
Second Big is 4

Output 4:
Enter the limit
8
Enter 8 numbers
1
5
9
3
7
4
6
8
First Big is 9
Second Big is 8

Find First and Second Biggest in N Numbers, without using Arrays: C Program


[youtube https://www.youtube.com/watch?v=gs4YT-Qcw6k]

YouTube Link: https://www.youtube.com/watch?v=gs4YT-Qcw6k [Watch the Video In Full Screen.]


Logic To Find First and Second Biggest Number in N Numbers, without using Arrays

First we ask the user to enter length of numbers list. If user enters limit value as 5, then we ask the user to enter 5 numbers. Once the user enters limit value, we iterate the while loop until limit value is 0. For each iteration of while loop we ask the user to enter a integer number. We check if the new number entered by the user is greater than fbig. If true, we swap the value of fbig to sbig and value of num to fbig. If the new number entered by the user is not greater than fbig, then we check if its greater than sbig. If true, we transfer the value of num to sbig.

Once the value of limit is 0, control exits the while loop, and inside fbig we have first biggest number from the list of numbers entered by the user and sbig will have the second biggest number from the list of numbers entered by the user.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert