This is a C Program to find the largest number in an array.
This program will implement a one-dimentional array and find out the largest element present in the array.
1. Create an array of fixed capacity.
2. Taking size of the array as input from users, run the for loop till the size to insert element at each location.
3. Considering the first element of the array to be the largest, compare all the remaining elements of array, and change the largest value if assumed largest element is smaller than the comparing element.
4. At last, the largest element will hold the actual largest value in the array. Thus, print it.
Here is source code of the C Program to find the largest number in an array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C Program to Find the Largest Number in an Array
*/
#include <stdio.h>
int main()
{
int array[50], size, i, largest;
printf("\n Enter the size of the array: ");
scanf("%d", &size);
printf("\n Enter %d elements of the array: ", size);
for (i = 0; i < size; i++)
scanf("%d", &array[i]);
largest = array[0];
for (i = 1; i < size; i++)
{
if (largest < array[i])
largest = array[i];
}
printf("\n largest element present in the given array is : %d", largest);
return 0;
}
1. Create an integer array of capacity 50.
2. Take size of the array as input from users.
3. Using for loop, take array element as input from users and insert into the array.
4. After inserting all the elements of the array, consider the very first element of array to be the largest.
5. Run a for loop, from 1 to arraySize-1, extracting array element one by one and comparing it to the largest element.
6. If largest element is smaller than the comparing element of array, then the largest element is updated to current element of array.
7. At the end, the largest element will hold the actual largest value present in the array.
Enter the size of the array: 5 Enter 5 elements of the array: 12 56 34 78 100 largest element present in the given array is : 100
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.