C

C Program To Find Largest Of N Numbers Using While Loop

In this tutorial, we are going to see how to write a C program to find the largest of N numbers using while loop. In the following example, we keep asking the user to enter a number(s) bigger than 0. If the user types 0, the program exit. At first, we consider the max as 0. Then in the loop, we compare it with each input entered by the user.
 

 

C Program To Find Largest Of N Numbers Using While Loop
#include <stdio.h>

int main(void)
{

    int n;
    int max = 0;

    printf("Enter a number (0 to exit): ");
    scanf("%d", &n);

    while (n != 0)
    {
        if (max < n)
        {
            max = n;
        }
        printf("Enter a number (0 to exit): ");
        scanf("%d", &n);
    }
    printf("Max is: %d", max);
}

Output:

Enter a number (0 to exit): 2
Enter a number (0 to exit): 5
Enter a number (0 to exit): 4
Enter a number (0 to exit): 3
Enter a number (0 to exit): 9
Enter a number (0 to exit): 1
Enter a number (0 to exit): 0
Max is: 9

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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