In the world of programming finding the largest element is a common task. In this article, we are going to write a C program to find the largest of n numbers.
Table of Contents
Objective
The objective of this tutorial is to write a C program that accepts n numbers as input and finds the largest among them.
Algorithm for Finding the Largest Number
To write a C program that efficiently finds the largest number among n numbers, We will use a simple algorithm. The steps of the algorithm are given below-
- Start the program.
- Declare Variables –
n
for a total count of numbers,num
to hold each input number, andlargest
to hold the largest of n numbers. - Start a loop to take n numbers as input.
- Compare num with largest, if
num>largest
assign num to largest that islargest = num
. - Display the largest number as the output.
- End of thev program.
C Program for Finding the Largest Number
Let’s implement the above algorithm using the C program.
#include <stdio.h>
void main()
{
int n, num, largest;
printf("Enter the total count of number");
scanf("%d", &n);
printf("Enter all the numbers one by one\n");
scanf("%d", &largest);
for (int i = 2; i <= n; i++)
{
scanf("%d", &num);
if (num > largest)
{
largest = num;
}
}
printf("Largest number = %d", largest);
}
Output
The above C program asks the user to input a total number of counts after that proceeds to accept n numbers one by one, using a loop. It compares each input number with the largest and updates the largest if the new number is greater.
3 thoughts on “Find the Largest of n Numbers Using the C Program”