This is a c program to find the largest element from an array. We have used the for loop and if… statement to achieve the program goal. In this program, firstly the user will enter the number of array elements and then the numbers of elements. On the basis of entered array elements, we will find the largest number and displays as output.
Copy the below c program and execute it with the help of c compiler. We have executed this program and added the program output at the below of this page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <stdio.h> int main() { int i, n; float arr[100]; printf("Enter the number of elements between(1-100):"); scanf("%d", &n); printf("\n"); // Stores number entered by the user for(i = 0; i < n; ++i) { printf("Enter Number %d: ", i+1); scanf("%f", &arr[i]); } // Loop to store largest number to arr[0] for(i = 1; i < n; ++i) { // Change < to > if you want to find the smallest element if(arr[0] < arr[i]) arr[0] = arr[i]; } printf("The largest element: %.2f", arr[0]); return 0; } |
Enter the number of elements between(1-100): 5
Enter Number 1: 8
Enter Number 2: 5
Enter Number 3: 6
Enter Number 4: 3
Enter Number 5: 19
The largest element is: 19.00
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: array in c, array in c language, c program, c program examples, c program examples with output, c programs, c programs with solutions, find largest element of an array in c, largest number from an array, list of important c programs