Thursday, 11 June 2020

Second Highest number from array using C language

Q : find second largest number from 1D array of n numbers.


Answer : 


#include<stdio.h>
void main()
{
     int n1,n2,ar[5],i;

     for(i=0;i<5;i++)
     {
          printf("Enter number %d number> ",i+1);
          scanf("%d",&ar[i]);
     }
     n1=ar[0];
     for(i=0;i<5;i++)
     {
          if(n1<ar[i])
          {
              n2=n1;
              n1=ar[i];
          }
     }
     /* printing */
     for(i=0;i<5;i++)
          printf("\t%d",ar[i]);
     printf("\n\n\tSecond Largest no. is %d",n2);

}

No comments:

Post a Comment