C program to find the largest number in an array

C program to find the largest number in an array

Problem Description

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
```

#include <stdio.h>

int main()
{
int i, n, largest;
int arr[50];

printf("Enter total number of elements(1 to 50): ");
scanf("%d", &n);
printf("\n");

// Stores number entered by the user
for(i = 0; i < n; i++)
{
printf("Enter Number %d: ", i+1);
scanf("%d", &arr[i]);
}

largest = arr[0];
// 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);

return 0;
}

```

Program Output

```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```

Program Explanation

```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.
```