C program to find largest element in array

C program to find largest element in array:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>

main()
{
   int a[30],i,num,large;

   printf("Enter no of elements :");
   scanf("%d",&num);

   //Read n elements in array
   printf("\nEnter %d elements in array",num);
   for(i=0;i<num;i++)
   {
      scanf("%d",&a[i]);
   }

   //Assuming first element as largest
   large=a[0];

   for(i=0;i<num;i++)
    {
      if(a[i] > large)
       {
         large=a[i];
       }
    }

   // Printing Result
   printf("\nLargest Element : %d",large);
}

OUTPUT:

Enter no of elements :5

Enter 5 elements in array 7 8 3 6 15

Largest Element : 15

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.