Find Biggest of N Numbers, without using Arrays: C Program


Write a C program to find biggest of N numbers without using Arrays and using while loop.

Related Read:
while loop in C programming

Logic To Find Biggest Number, without using Arrays

We ask the user to enter the limit. i.e., the length of the list of numbers. For Example, if user enters limit value as 5, then we accept 5 numbers from the user and then find the biggest and output that number on to the console window.

First we ask the user to enter the limit. If user enters limit value as 5, we iterate the while loop 5 times i.e., until value of limit is 0. Inside the while loop, for each iteration we ask the user to enter a number. Next we check if that user entered number is greater than the value present in variable big. If the new number entered by the user is greater than value present in variable big, then we copy assign the new number entered by the user to the variable big. We keep doing this for each number entered by the user. Simultaneously we keep decrementing the value of variable limit by one for each iteration of while loop. Once limit is 0, control exits the while loop and we print the value present inside the variable big.

Source Code: Find Biggest of N Numbers, without using Arrays: C Program

 
#include<stdio.h>

int main()
{
    int limit, num, big = 0;

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

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

    scanf("%d", &num);
    big = num;
    limit = limit - 1;

    while(limit > 0)
    {
        scanf("%d", &num);
        if(num > big)
        {
            big = num;
        }
        limit--;
    }

    printf("Biggest number is %d\n", big);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5
Biggest number is 5

Output 2:
Enter the limit
5
Enter 5 numbers
5
4
3
2
1
Biggest number is 5

Output 3:
Enter the limit
5
Enter 5 numbers
1
2
5
3
4
Biggest number is 5

Output 4:
Enter the limit
10
Enter 10 numbers
1
5
9
3
7
5
6
14
1
1
Biggest number is 14

Find Biggest of N Numbers, without using Arrays: C Program


[youtube https://www.youtube.com/watch?v=Cx4-egvgSQs]

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


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

Leave a Reply

Your email address will not be published. Required fields are marked *