C Program to Find Largest Element From Array


Question: Write a program in C to read an array containing n numbers and find largest element from it.

C Program to Find Largest Element From Array


#include<stdio.h>

int main()
{
 float a[50], lg;
 int i, n;

 printf("Enter n: ");
 scanf("%d", &n);
 
 /* Reading array */
 printf("Enter numbers:\n");
 for(i=0;i< n;i++)
 {
  printf("a[%d] = ",i);
  scanf("%f", &a[i]);
 }
 
 /* Finding large */
 lg = a[0];
 for(i=0;i< n;i++)
 {
  if(a[i]>lg)
  {
   lg = a[i];
  }
 }
 printf("\nLarge = %f", lg);
 return 0;
}

Output

Enter n: 5↲
Enter numbers:
a[0] = 21↲
a[1] = 0↲
a[2] = -78↲
a[3] = 77↲
a[4] = 9↲

Large = 77.000000