This program takes n numbers (where n is between 1 to 50 and is specified by the user) from the user and stores them in an array. Then the program iterate the array to find the largest number stored in it.
C program to find the largest number in an array - Source code
int i, n, largest;
printf("Enter total number of elements(1 to 50): ");
// Stores number entered by the user
for(i = 0; i < n; i++)
printf("Enter Number %d: ", i+1);
largest = arr;
// Loop to store largest number to variable largest
for(i = 1; i < n; i++)
if(arr[i] > largest)
largest = arr[i];
printf("Largest element = %d", largest);
Enter total number of elements(1 to 50): 8
Enter Number 1: 2354
Enter Number 2: 754
Enter Number 3: 333
Enter Number 4: 889
Enter Number 5: 45
Enter Number 6: 48
Enter Number 7: 12
Enter Number 8: 3569
Largest element = 3569
1. This program takes n number of elements from user and stores it in array arr. The maximum number of elements the array arr can hold is 50.
2. To find the largest element, the first element of array is stored in variable largest.
3. Then, each element of array is traversed using a for loop(starting with second element) and compared to the number stored in the largest. If the number is found to be greater than the largest variable, value of that number is assigned to the largest.
4. This process continues until last element is compared with the number stored in the largest variable.
Install Computer Science Bits Android app to access all study material on the go.