C Program to Find Largest and Smallest Element in an Array

Get Certified in C Programming and Take Your Skills to the Next Level

Program 1

//Largest element from array
#include<stdio.h>
#include<conio.h>
int main()
{
       system("cls");
         int ar[500],n,i,max,pos;
         printf("\n Enter the limit:");
         scanf("%d",&n);
         printf("Enter elements in array:\n");
         for(i=0;i<n;i++)
         scanf("%d",&ar[i]);
         max=ar[0];
         //max=0;
         pos=0;
         for(i=1;i<n;i++)
         {
             if(ar[i]>max)
             {
                max=ar[i];
                pos=i;
             }
         }
         printf("\n Largest element is : %d",max);
         printf("\n Poisition is : %d",pos+1);
       return 0;
}

Program 2

//Largest element from array
#include<stdio.h>
#include<conio.h>
int main()
{
       system("cls");
         int ar[500],n,i,min,pos;
         printf("\n Enter the limit:");
         scanf("%d",&n);
         printf("Enter elements in array:\n");
         for(i=0;i<n;i++)
         scanf("%d",&ar[i]);
         min=ar[0];
         //max=0;
         pos=0;
         for(i=1;i<n;i++)
         {
             if(ar[i]<min)
             {
                min=ar[i];
                pos=i;
             }
         }
         printf("\n Smallest element is : %d",min);
         printf("\n Poisition is : %d",pos+1);
       return 0;
}

 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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