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 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